tags:

views:

174

answers:

2

I'd like the quickest way (in excel VBA) to identify whether one string occurs anywhere within another - an 'includes' function perhaps?

+13  A: 

I believe INSTR() is the function; if it returns anything other than zero, the string is found.

exists = InStr("avbprogram", "vb") <> 0
Carl Manaster
Beat me to it :P
MitMaro
+8  A: 

Carl is correct but you should also know that the default compare option for InStr is case-sensitive. If you want to do case-insensitive checks you should either wrap your arguments in LCase/UCase or use the extended form of the InStr function as shown below:

exists = InStr(1, "avbprogram", "vb", vbTextCompare)

where the first argument is the index of the first character to start comparing from and the last argument indicates case-insensitive comparison. The short-hand that Carl showed is actually equivalent to what is shown below:

exists = InStr(1, "avbprogram", "vb", vbBinaryCompare)
tchester
thanks to both of you for your help, much appreciated. R