PDA

View Full Version : Solved: how to find the index of an element in array?



jimliu
11-12-2005, 10:33 PM
Is there something like indexOf() for an array in VBA 2003?
Is there something like len() to get the length of an array?

I need to get the index of an element in array, and the length of the array. Any solution is appreciated.

Jacob Hilderbrand
11-12-2005, 11:14 PM
To get the Index you will need to loop.

For i = LBound(MyArray) To UBound(MyArray)
If MyArray(i) = "ThisValue" Then
Index = i
Exit For
End If
Next i

Msgbox Index


For length of the array do you mean the number of items or the actual total length of all items. For the first on just use UBound, for the second, loop and add up the Len of each item.

jimliu
11-13-2005, 12:10 AM
Thank you very much DRJ. Your timely answer solved my problem. I was looking for something like indexOf(). When I could not find it, I tried to implement one myself. Then I could not find something like len(). Now I see that actually, UBound() is the answer.