Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default VBA function : How to search a string in another string?

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
  #3   Report Post  
Posted to microsoft.public.excel.programming
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,560
Default VBA function : How to search a string in another string?

Hi,
This I think will work, but you will have to expand the case to cover all of
the state codes. It puts a 1 or 0 in ReturnValue, but it does not do anything
with it?:

Sub USCity()
TwoRight = Right(ActiveCell.Value, 2)
ThreeRight = Right(ActiveCell.Value, 3)
Left1 = Left(ThreeRight, 1)
If Left1 = " " Then
Select Case TwoRight
Case "NY"
ReturnValue = 1
End Select

Else
ReturnValue = 0
End If

End Sub

"bibi-phoque" wrote:

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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default VBA function : How to search a string in another string?

Add this UDF

Function StringFound(val, aryValues)
Dim i As Long
Dim aryTmp
If TypeName(aryValues) = "Range" Or _
VarType(aryValues) vbArray Then
aryTmp = aryValues
Else
StringFound = "Error in lookup values"
Exit Function
End If
For i = LBound(aryTmp) To UBound(aryTmp)
If val Like "*" & aryValues(i, 1) & "*" Then
StringFound = 1
Exit Function
End If
Next i
StringFound = 0
End Function


and use like so

1) Create a range of state ids, named say States, and a formula of

=IF(stringfound("This state NM",States),1,0)

2) Include the states in the formula

=IF(stringfound("This state NM",{"AL";"AZ";"CA";"NM"}),1,0)

--

HTH

RP
(remove nothere from the email address if mailing direct)


"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





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default VBA function : How to search a string in another string?

Thanks a lot!
This is working perfectly.

Appreciate your help.

Regards,
Yann

*** Sent via Developersdex http://www.developersdex.com ***
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search for string containing richzip Excel Discussion (Misc queries) 5 January 14th 10 09:49 PM
How to search a string from the right ? EuroMaverick Setting up and Configuration of Excel 2 March 28th 07 09:20 PM
to search for a string and affect data if it finds the string? Shwaman Excel Worksheet Functions 1 January 11th 06 12:56 AM
Q: search string JIM.H. Excel Programming 1 January 5th 05 09:09 PM
Search for certain string Ryan[_9_] Excel Programming 1 May 10th 04 04:13 PM


All times are GMT +1. The time now is 01:31 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"