View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Ted M H Ted M H is offline
external usenet poster
 
Posts: 83
Default Check if variable contains a string

This works perfectly (I used the case sensitive test). I don't yet
understand why, but my immediate problem is solved.

I spent a little time with VBA help on the InStr function, and I think I
understand how it works. What puzzles me is the 2 +.... and the 0....
additions to the statement. You've already solved my problem, but if you're
inclined to help educate a struggling programming hacker further I'd sure be
interested in understanding why the solution works.

Many thanks.

"Rick Rothstein" wrote:

If I understand your question correctly...

W = 2 + (InStr(VariableX, VariableY) 0)

The InStr function, as I used it above, is case sensitive. If you want a
case insensitive test...

W = 2 + (InStr(1, VariableX, VariableY, vbTextCompare) 0)

--
Rick (MVP - Excel)


"Ted M H" wrote in message
...
I'm trying to write VBA code that will look at a String variable and tell
me
whether or not that String contains another String €” without erroring out.
Variable X contains this string: =SUM(AAAA,BBBB)
Variable Y contains this string: PPPP
I want something that works sort of like this:
If Variable X contains Variable Y, Then Variable W = 1
If Variable X does not contain Variable Y, Then Variable W = 2
In the example above, W will equal 2, since X does not contain Y
If Y is BBBB then W will equal 1, because X does contain Y
Many thanks.