View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Check if variable contains a string

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.