View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_6_] Bob Phillips[_6_] is offline
external usenet poster
 
Posts: 11,272
Default Is member of an array?

Shatin,

Here is one method that will work in VBA, but not from a worksheet

Function InArray(thisValue, thisArray) As Boolean
Dim c As Range

Range("A" & Rows.Count).Resize(1, UBound(thisArray)).Value = thisArray
Set c = Range("A" & Rows.Count).Resize(1,
UBound(thisArray)).Find(thisValue)
InArray = Not c Is Nothing

End Function


Example call

If InArray(4, myArray) Then
...

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Shatin" wrote in message
om...
Let's say I have an array with 100 string elements. Now I have to
check if a particular string is same as one of the elements in the
array, is there a simple way of doing so instead of using many
If/Elseif statements?

If not, I suppose one would have to use some kind of lookup functions.
In that case, does it necessarily mean one would have to include a
data worksheet (invisible or otherwise) in the file for the lookup?

For example, there are 100 string elements in column H. I then create
the newArray by applying various left/mid/right functions to column H.
How do I check if strVariable is same as one of the elements in
newArray? Do I have to list all the elements of newArray on a data
worksheet first?

TIA