Method that finds string1 inside string2?

R

Rick Charnes

Is there a VBA function or method that will return the position of one
string inside another string? Thanks much.
 
G

Greg Maxey

InStr

Something like:

Sub ScratchMacro()
Dim pStr1 As String
Dim pStr2 As String
pStr1 = "Always Vote Repulican"
pStr2 = "Vote"
MsgBox pStr2 & " begins at position " & InStr(pStr1, pStr2) _
& " of the string " & pStr1
End Sub
 
J

Jean-Guy Marcil

Rick Charnes was telling us:
Rick Charnes nous racontait que :
Is there a VBA function or method that will return the position of one
string inside another string? Thanks much.

For example:

Dim strOne As String
Dim strTwo As String
Dim i As Long

strOne = "This is an actual string"
strTwo = "actual"

i = InStr(1, strOne, strTwo)


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

Klaus Linke

Hi Greg,

You can even use it to turn a false statement like pStr1 into a true
statement:

Sub ScratchMacro2()
Dim pStr1 As String
Dim i As Long
pStr1 = "Always Vote Republican"
i = InStr(pStr1, "Republican")
MsgBox left(pStr1, i-1), vbInformation
End Sub

SCNR
Klaus
 
G

Greg Maxey

But turning one more time:

Sub ScratchMacro2()
Dim pStr1 As String
Dim pTruth As String
Dim i As Long
pStr1 = "Always Vote Republican"
i = InStr(pStr1, "Republican")
pTruth = Left(pStr1, i - 1)
MsgBox pHalfTruth & ", but never for a Democrat", vbExclamation
End Sub

;-)
 
G

Greg Maxey

Opps,

That would be Msgbox pTruth & ", but never for a Democrat", vbExclamation
 

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