Return Matrix as Range From Function

  • Hi


    I'm trying to write some code to return a matrix as a range from a function. (The reason is that I already have functions that take in ranges as input, and i want to use this function there)



    I think the problem lies in the line


    Code
    correlrange.Resize(3, 3) = Correls


    and i don't know how to transfer the array to a range. The tips i tried to modify from the forum don't seem to work

  • Re: Array From A Range And Then Returning It From Function


    mechanik,


    welcome to the forum.


    you probably miss the array definition "()" for correlrange

    Code
    Dim correlrange [B][COLOR="Red"]()[/COLOR][/B]As Range


    filippo

  • Re: Array From A Range And Then Returning It From Function


    When using objects, such as ranges, you need to use the SET command.


    Code
    Set correlrange.Resize(3, 3) = Correls

    [h4]Cheers
    Andy
    [/h4]

  • Re: Array From A Range And Then Returning It From Function


    HI Andy,


    wrong tags. this time I cannot edit for you!!: D


    filippo

  • Re: Return Matrix as Range From Function


    This line of code would write the contents of Correls to correlrange on the worksheet, except that is Nothing when you execute it; it has not been initialized to point to any range:

    Code
    correlrange.Resize(3, 3) = Correls


    If your intent is to set it to point to the array, as in

    Code
    Set correlrange.Resize(3, 3) = Correls


    then I'd expect it to fail on the Resize (because it isn't defined, so there's nothing to resize), and if you got past that, it would fail on the assignment (because it's a type mismatch).

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

  • Re: Return Matrix as Range From Function


    I just noticed Correls is an array not a range so the set will not work at that point.


    Code
    Set correlrange = Range("A1").Resize(3, 3)
    correlrange = Correls

    [h4]Cheers
    Andy
    [/h4]

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!