Consulting

Results 1 to 4 of 4

Thread: Function returning Arrays

  1. #1

    Function returning Arrays

    Function NewVector() As Double()
    Dim Vector(10) As Double 'Or any size but MUST be double
    ...
    ...
    NewVector = Vector
    End Function

    Problem:
    Excel does not display the Function results (through the regular area selection and Ctrl-Shft-Enter).
    It comes out with the error #VALUE! in every selected cell.

    If I change the Function type to Variant the compiler says "cannot assign to array" (it means the Function).

    I cannot change the Vector type. Must be Double.

    Any hint, please...? Thanks!

  2. #2
    VBAX Regular
    Joined
    Jun 2008
    Posts
    64
    Location
    I've always written functions returning arrays as:

    Function NewVector() As Variant
    'You can Dim it whatever here
    ....
    ....
    NewVecor = Vector
    End Function


    Is there a reason this wouldn't return as a double for you?

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Why must it be a Doule, Varaiant will hold whatever type you want.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    [VBA]NewVector = Vector[/VBA]
    If newVector is type Double() or Variant() this will give an error. (NewVector() is an array of values, but NewVector is not a variable. Its almost like there is nothing called NewVector, only NewVector(1), NewVector(2), etc. )
    Inside a function called from a spreadsheet, that will result in #VALUE.

    If NewVector is Variant, it can accept the value of an array.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •