Searching for value in array

N

neil

Hi,

I've been trying to find a value in array (within a Function). This
should be quite easy and straightforward, but I'm obviously going wrong
somewhere...

I'm looping through an array, and trying to match a value (passed as a
variant to the function) within the array.

Code:
[gY = value passed to function]
****************************************************************
dim yield(20) as variant, gilt_index as variant, i as Long

yield(0) = 3
.........................................(1)(2), etc
yield(20) = 8


For i = LBound(yield) To UBound(yield)


If gY = yield(i) Then gilt_index = yield(i)


Next
********************************************************************************

Within this loop, I can never match the value (gY) to a value in the
array, even though I know it is in the array. It simply does not match
it.

Any thoughts? This is driving me nuts...
(p.s. I'm using Office 2000)

Thanks
Neil.
 
D

Doug Robbins - Word MVP

It seems that gY must be declared as a string. In the following, the MsgBox
returns 4.

Dim yield As Variant, gilt_index As Variant, i As Long, gY As String

yield = Split("1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20")

gY = 4

For i = LBound(yield) To UBound(yield)
If gY = yield(i) Then
gilt_index = yield(i)
MsgBox gilt_index
End If
Next

or use

Dim yield As Variant, gilt_index As Variant, i As Long, gY As Variant

yield = Split("1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20")

gY = 4

For i = LBound(yield) To UBound(yield)
If gY = Val(yield(i)) Then
gilt_index = yield(i)
MsgBox gilt_index
End If
Next

or

Dim yield As Variant, gilt_index As Variant, i As Long, gY As Variant

yield = Split("1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20")

gY = 4

For i = LBound(yield) To UBound(yield)
If Format(gY) = Format(yield(i)) Then
gilt_index = yield(i)
MsgBox gilt_index
End If
Next

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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