Consulting

Results 1 to 5 of 5

Thread: Returning Arrays From Functions

  1. #1

    Returning Arrays From Functions

    I come from a Java background - Can anyone explain why this is disallowed?

    And how best to achieve this?

    Public Function testMe() As Variant()
        ReDim abc(0 To 1, 0 To 1) As Long
        
        testMe = abc
    End Function

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    [VBA]
    Sub test()
    Dim t, msg
    For Each t In testMe
    msg = msg & t
    Next
    MsgBox msg
    End Sub

    Public Function testMe() As Variant
    ReDim abc(0 To 1, 0 To 1) As Long
    abc(0, 0) = 1
    abc(1, 0) = 2
    abc(0, 1) = 3
    abc(1, 1) = 4
    testMe = abc
    End Function

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Thanks - But why does this work conceptually?

    How can you declare a function that returns a variant() - Notice the array on the signature.

    Thats my question

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    Sorry, I'm not much on theory. I'm sure someone else will come along though.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Anyone please? I am keen to know the answer here...

    Thanks

Posting Permissions

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