View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default VBA function : How to search a string in another string?

Something like this:

Public Function ContainState(rng As Range)
Dim vArr As Variant
Dim i As Long
Dim sStr As String
Dim bFound As Boolean
vArr = Array("AL","AK", ... )
sStr = rng(1).Text
For i = LBound(vArr) To UBound(vArr)
If InStr(1, sStr, " " & vArr(i), vbBinaryCompare) 0 Then
bFound = True
Exit For
End If
Next
If bFound Then
ContainState = 1
Else
ContainState = 0
End If
End Function

--
Regards,
Tom Ogilvy

"bibi-phoque" wrote in message
om...
Hi all,
I am a beginner in VBA, and I need a function that return 1 or 0
depending if my string contains one of the US states.

To explain a bit more :

If my cell = "East Syracuse, NY"
I want to return 1 because there is a US state in the string.
If my cell = Karlsruhe, Germany
I want to return 0


I have a list of all the US states (2 letter code).
For the function, I don't mind if it is a big "if then" with all the
50 conditions (number of US states).

I am aware of the FIND function in Excel, which does what I need, but
the formula is too big (I need to compare with the 50 US states), it's
why I need a macro. And FIND is not a vba function...
One more thing : it has to be case sensitive.

Any help would be appreciated.

Thanks in advance,
Yann