Passing arrays within arrays

J

Jeremy_LFW

Hi,
I've created a search function for arrays, but it only works on 1-d arrays.

dim myarray(1 to 5, 1 to 10) as string

msgbox mysearch((myarray(1)), "mysearchstring")

is it possible to have VBA treat each 10-element array as if it were an
array? the above gets a compile-time error saying it is not an array.

thanks
Jeremy
 
R

RB Smissaert

Yes, very much possible:

Sub test()

Dim arrMain(1 To 3) As Variant
Dim arr1(1 To 10) As String
Dim arr2(1 To 10) As String
Dim arr3(1 To 10) As String
Dim arr

arrMain(1) = arr1
arrMain(2) = arr2
arrMain(3) = arr3

arrMain(2)(3) = "find this"

MsgBox SearchArrayOfArrays("find this", arrMain)(1), , _
SearchArrayOfArrays("find this", arrMain)(2)

End Sub

Function SearchArrayOfArrays(strToFind As String, arrToSearch As Variant) As
Variant

Dim i As Long
Dim n As Long
Dim arrResult(1 To 2)

For i = LBound(arrToSearch) To UBound(arrToSearch)
For n = LBound(arrToSearch(i)) To UBound(arrToSearch(i))
If arrToSearch(i)(n) = strToFind Then
arrResult(1) = i
arrResult(2) = n
SearchArrayOfArrays = arrResult
Exit Function
End If
Next
Next

End Function

RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top