How to declare function that returns array of dynamic size
How to declare function that returns array of dynamic size
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 3 of 3

Thread: How to declare function that returns array of dynamic size

  1. #1
    New Member
    Join Date
    Mar 2002
    Location
    Forest, VA
    Posts
    49
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

     
    Hi,

    I searched the documentation I have and this web site but, I still cannot figure out how to declare a function that returns an array (in my case a array of strings) of an un-predetermined size. Currently, I use "Public Dim myArray() As String" and then ReDim the array in my Sub. This works but I would much rather declare the Sub as a Function that returns the array of strings. Does anyone know how to do this? Thanks in advance for the help.

    Regards,
    Vlip

  2. #2
    MrExcel MVP Damon Ostrander's Avatar
    Join Date
    Feb 2002
    Location
    Denver, Colorado USA
    Posts
    4,240
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Hi Vlip,

    Simply declare the function as Variant, then assign the array to the function value. A function cannot return an array. However, a function CAN return a variant that CONTAINS an array. I realize that this is a vague distinction, but it workds. For example:

    Function StrArray( args.. ) As Variant

    Dim myArray() As String

    ReDim myArray(50)

    'code that sets values into myArray and/or
    'ReDims myArray

    StrArray = myArray

    End Function
    Keep Excelling.

    Damon

    VBAexpert Excel Consulting
    LinkedIn Profile http://www.linkedin.com/pub/damon-ostrander/7/79/a93
    AllExperts Profile http://www.allexperts.com/ep/1059-30...-Ostrander.htm

  3. #3
    New Member
    Join Date
    Mar 2002
    Location
    Forest, VA
    Posts
    49
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    Thank you very much! I knew I could do what you suggested. I guess the answer I was looking for is "a function cannot return an array". It seems to me a limitation of VB.

    Vlip

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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

 

 
DMCA.com