PDA

View Full Version : Solved: Identify array element



clvestin
01-02-2006, 06:04 AM
I have a 1-dim array:

runarray=array(1,3,2,6,4,12,8,9)

I read a value that i know corresponds to one of the elements of this array.
How do i identify which one? ie if i read a 12, i need to produce the 5 as in runarray(5).

None of my references have led me in the right direction, my immediate window is full of interesting stuff found on the way to other stuff, but...
Appreciate the help

Bob Phillips
01-02-2006, 06:56 AM
Sub ArrayQ()
Dim runarray
Dim i As Long

runarray = Array(1, 3, 2, 6, 4, 12, 8, 9)
For i = LBound(runarray) To UBound(runarray)
If runarray(i) = 12 Then
MsgBox "Item " & i
Exit For
End If
Next i
End Sub

Dave
01-02-2006, 07:02 AM
I'm not real sure what you are trying to achieve but maybe this example will help. Dave

Sub TestArray()
Dim Runarray(8) As Variant, Cnt As Integer
Runarray(0) = 1
Runarray(1) = 3
Runarray(2) = 2
Runarray(3) = 6
Runarray(4) = 4
Runarray(5) = 12
Runarray(6) = 8
Runarray(7) = 9
For Cnt = LBound(Runarray) To UBound(Runarray) - 1
If Runarray(Cnt) = 12 Then
MsgBox "The array position is " & Cnt
Exit For
End If
Next Cnt
End Sub

mdmackillop
01-02-2006, 02:51 PM
Hi,
To avoid the loop, you could use
i = 12
runarray = Array(1, 3, 2, 6, 4, 12, 8, 9)
MsgBox "Item " & Application.WorksheetFunction.Match(i, runarray)

Note that this will return 6, not 5 for this example
Regards
MD

clvestin
01-03-2006, 08:33 AM
Just a quick correction: Match(i,runarray,0)
the zero dictates an exact match.

Thx for the help