+ Reply to Thread
Results 1 to 3 of 3

Function that return Array

Hybrid View

yellowpower Function that return Array 07-08-2011, 04:22 AM
TMS Re: Function that return Array 07-08-2011, 04:35 AM
snb Re: Function that return Array 07-08-2011, 06:08 AM
  1. #1
    Registered User
    Join Date
    11-05-2010
    Location
    london, england
    MS-Off Ver
    Excel 2003
    Posts
    59

    Function that return Array

    Hi Guys,

    I am trying to construct an array in a function and return in but it doesnt work.
    for example, the below function contruct an array
    Function test()
    
    redim myarray (1 to 5)
    
    dim i
    
    for i=1 to 5
    myarray(i)=i
    next i
    
    test=myarray
    End Function

    Then in a sub, I try this

    
    sub mysub()
    redim myarray(1 to 5)
    myarray=test()
    end sub
    But it is not working? any idea how I could achieve this?
    Thanks

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,359

    Re: Function that return Array

    What makes you think it's not working?

    Option Explicit
    
    Function test()
    ReDim myarray(1 To 5)
    Dim i
    For i = 1 To 5
        myarray(i) = i
    Next i
    test = myarray
    End Function
    
    Sub mysub()
    ReDim myarray(1 To 5)
    myarray = test()
    End Sub

    Stepping through ...

    ?test(1)
     1 
    ?test(2)
     2 
    ?test(3)
     3 
    ?test(4)
     4 
    ?test(5)
     5 
    ?myarray(1)
     1 
    ?myarray(2)
     2 
    ?myarray(3)
     3 
    ?myarray(4)
     4 
    ?myarray(5)
     5

    What's it doing, or not doing, that it shouldn't do, or should do?


    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Function that return Array

    or to keep it simple:

    Sub snb_000()
      For Each cl In snb_001
        MsgBox cl
      Next
    End Sub
    
    Function snb_001()
      snb_001 = Split("aa|bb|cc|dd|ee", "|")
    End Function
    Last edited by snb; 07-08-2011 at 06:12 AM.



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1