VBScript Index Array Location

This is hopefully easy, but I cannot find a link to it.

How to print the location of what is stored in the array, and not the actual element that is in it.

Array(0) = Dog Array(1) = Cat Array(2) = Fish 

Lets say that I was looking for an array and found cat, how I print the location where cat was stored, in this case Index Number (1).

Thanks in advance.

+4
source share
2 answers

The location of the array is called index

If you run a loop,

 For i = LBound(Array) to UBound(Array) if Array(i) = "Cat" then '--restrict to find index of particular item MsgBox i '-- gives the location/index of Cat item End if next i 
  • LBound : this is the lower bound, the starting index of the array. First. This can be zero or anything, since VBA provides the flexibility to change the default array base as 0 or 1.

  • UBound : upper bound, end index of the array. Last thing.

For further reading: LBound and Ubound conflicts in the case of an array that has been assigned by a range .

+4
source

It is better to use an ArrayList for such cases than a simple array.

 Set myArray = CreateObject ("System.Collections.ArrayList") With myArray .Add "Dog" .Add "Cat" .Add "Fish" End With intIndex = myArray.IndexOf ("Cat",0) 

In addition, you do not need to worry about boundaries.

+4
source

All Articles