Consulting

Results 1 to 3 of 3

Thread: Solved: how to find the index of an element in array?

  1. #1
    VBAX Regular
    Joined
    Nov 2005
    Posts
    9
    Location

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

    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.

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    To get the Index you will need to loop.

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

    Msgbox Index
    [/VBA]

    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.

  3. #3
    VBAX Regular
    Joined
    Nov 2005
    Posts
    9
    Location
    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.

Posting Permissions

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