+ Reply to Thread
Results 1 to 8 of 8

Passing 2 Dimensional Arrays to functions

Hybrid View

keithabailey Passing 2 Dimensional Arrays... 09-23-2011, 04:06 AM
snb Re: Passing 2 Dimensional... 09-23-2011, 04:20 AM
keithabailey Re: Passing 2 Dimensional... 09-23-2011, 04:42 AM
snb Re: Passing 2 Dimensional... 09-23-2011, 04:46 AM
keithabailey Re: Passing 2 Dimensional... 09-23-2011, 04:54 AM
Colin Legg Re: Passing 2 Dimensional... 09-23-2011, 04:57 AM
snb Re: Passing 2 Dimensional... 09-23-2011, 05:02 AM
keithabailey Re: Passing 2 Dimensional... 09-23-2011, 05:28 AM
  1. #1
    Registered User
    Join Date
    12-10-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    41

    Passing 2 Dimensional Arrays to functions

    Hi,

    I have a 2d array with the first dimension being strings and the second dimension being integers.

    E.g. {(Scenario,1),(Time,4),(Group,8),(Client,10),(Line,3)}


    How can I pass this to a function?

    I ask as I can only find examples of where the types in the array are of the same nature, i.e. Long and Long.

    Any help would be appreciated
    Last edited by keithabailey; 09-23-2011 at 05:30 AM. Reason: Solved

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

    Re: Passing 2 Dimensional Arrays to functions

    Every range is a 2 dimensional array.
    You can pass a range or an array, that doesn't matter, nor does the content.

    sub snb()
      x = tst1(range("A1:K10"))
      sn=range("A1:K10").value
      msgbox tst2(sn)
    end sub
     
    function tst1(sn as range)
      tst1=sn.cells.count
    end function
     
    function tst2(sq)
     for j=1 to ubound(sq)
      tst2=join(application.index(sq,j))&vbcr
     next
    end function



  3. #3
    Registered User
    Join Date
    12-10-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Passing 2 Dimensional Arrays to functions

    I actually have it as an array though, as it is worked out from another set of data. Is there anyway to pass the array, or do I need to put the array into a range and then back to an array?

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

    Re: Passing 2 Dimensional Arrays to functions

    Please read my post again ( and did you test it ?):

    This is the array part: ( sn is an array)

     
    sub snb()
      sn=range("A1:K10").value
      msgbox tst2(sn)
    end sub
     
    function tst2(sq)
      for j=1 to ubound(sq)
       tst2=join(application.index(sq,j))&vbcr
      next
    end function

  5. #5
    Registered User
    Join Date
    12-10-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Passing 2 Dimensional Arrays to functions

    I have Option Explicit enabled, so would need to to define the data types.

    In the

    function tst2(sq)
    what should I be declaring the"sq" as?

  6. #6
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Passing 2 Dimensional Arrays to functions

    Hi,

    If the array contains both strings and longs, then it must be of type Variant.
    When you pass arrays in VBA, they must be passed ByRef.

    Does this code clarify things for you?

    Sub MyTest()
    
        Dim varrExample(0 To 2, 0 To 1) As Variant
        Dim blnResult As Boolean
        
        'let's put some data in our array for this example
        varrExample(0, 0) = "hello"
        varrExample(1, 0) = "the"
        varrExample(2, 0) = "world"
        
        varrExample(0, 1) = 1
        varrExample(1, 1) = 2
        varrExample(2, 1) = 3
        
        
        'let's pass this into our test function
        blnResult = MyFunction(varrExample)
        
        
    End Sub
    
    
    Function MyFunction(ByRef GiveMeAnArray() As Variant) As Boolean
    
        'do some stuff with the array
        
        
        
        'pass a result back, in this case a boolean
        MyFunction = True
        
    End Function

    I have Option Explicit enabled, so would need to to define the data types.
    This is a very good habit - one which I strongly recommend.
    Hope that helps,

    Colin

    RAD Excel Blog

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

    Re: Passing 2 Dimensional Arrays to functions

    Please remove option explicit.
    The code will run flawlessly.
    Your question is about passing variables, not on declaring variables.

    maybe simpler as an illustration:

    Sub tst()
     Dim sn(3, 2)
    
     For j = 0 To 3
      For jj = 0 To 1
       sn(j, jj) = IIf(jj = 0, "text", 5)
      Next
     Next
       
     MsgBox tst2(sn)
    End Sub
    
    Function tst2(sq)
     For j = 0 To UBound(sq)
      tst2 = tst2 & sq(j, 0) & "_" & sq(j, 1) & "_" & sq(j, 2) & vbLf
     Next
    End Function
    Last edited by snb; 09-23-2011 at 05:36 AM.

  8. #8
    Registered User
    Join Date
    12-10-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Passing 2 Dimensional Arrays to functions

    Thanks for your help. That worked exactly as I was hoping! On to the next problem...

+ 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