Instr() with multiple search terms

E

ExcelMonkey

I it possible to use the Instr() function with multiple search terms. The
idea being that all below should return a value fo 1

Dim Y as Double

Y = Instr(1, "Hello World", "Hello|"The"|"Dog")
Y = Instr(1, "The World", "Hello|"The"|"Dog")
Y = Instr(1, "Dog World", "Hello|"The"|"Dog")

The idea here being to find matches at the beginning of the Target String.

Alternatively I could use Regular Expressions. But I cannot remember how to
use the Match functionality to return the position of the word.

Thanks

EM
 
D

Don Guillett

As far as I remember, you would have to use an if or select case

Sub ins()
If InStr(1, "Hello World", "Hello") = 1 Or _
InStr(1, "Hello World", "dog") = 1 Then y = 1
MsgBox y
End Sub
 
J

Jacob Skaria

OR..this way

Sub ins()
Dim strData As String
strData = "Hello world"
y = InStr(1, strData, "Hello", vbTextCompare) + _
InStr(1, strData, "the", vbTextCompare) + _
InStr(1, strData, "dog", vbTextCompare)
MsgBox y
End Sub

If this post helps click Yes
 
R

Rick Rothstein

I think you should change your assignment to the 'y' variable to something
like this...

y = Abs((InStr(1, strData, "Hello", vbTextCompare) + _
InStr(1, strData, "the", vbTextCompare) + _
InStr(1, strData, "dog", vbTextCompare)) > 0)

Otherwise, a value of more than 1 would be possible; and not the 2 you might
be thinking in, for example, this...

strDate = "Hello to the world"
 
J

Jacob Skaria

Rick; i meant to say

y = InStr(1, strData, "Hello", vbTextCompare) + _
InStr(1, strData, "the", vbTextCompare) + _
InStr(1, strData, "dog", vbTextCompare)
If y > 0 then
'Do something
End If
End Sub

If this post helps click Yes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top