ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Instr() with multiple search terms (https://www.excelbanter.com/excel-programming/433985-instr-multiple-search-terms.html)

ExcelMonkey

Instr() with multiple search terms
 
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

Don Guillett

Instr() with multiple search terms
 
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


--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"ExcelMonkey" wrote in message
...
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



Jacob Skaria

Instr() with multiple search terms
 
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
---------------
Jacob Skaria


"ExcelMonkey" wrote:

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


Rick Rothstein

Instr() with multiple search terms
 
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"

--
Rick (MVP - Excel)


"Jacob Skaria" wrote in message
...
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
---------------
Jacob Skaria


"ExcelMonkey" wrote:

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



Jacob Skaria

Instr() with multiple search terms
 
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
---------------
Jacob Skaria


"Rick Rothstein" wrote:

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"

--
Rick (MVP - Excel)


"Jacob Skaria" wrote in message
...
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
---------------
Jacob Skaria


"ExcelMonkey" wrote:

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





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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com