Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2007

Tip: Looking for answers? Try searching our database.

Method that finds string1 inside string2?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rick Charnes - 04 Apr 2007 20:26 GMT
Is there a VBA function or method that will return the position of one
string inside another string?  Thanks much.
Greg Maxey - 04 Apr 2007 20:36 GMT
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

On Apr 4, 3:26 pm, Rick Charnes <rickxyz--
nospam.zyxchar...@thehartford.com> wrote:
> Is there a VBA function or method that will return the position of one
> string inside another string?  Thanks much.
Klaus Linke - 04 Apr 2007 22:10 GMT
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

> InStr
>
[quoted text clipped - 13 lines]
>> Is there a VBA function or method that will return the position of one
>> string inside another string?  Thanks much.
Greg Maxey - 04 Apr 2007 22:27 GMT
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

;-)

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Hi Greg,
>
[quoted text clipped - 29 lines]
>>> Is there a VBA function or method that will return the position of
>>> one string inside another string?  Thanks much.
Greg Maxey - 04 Apr 2007 22:34 GMT
Opps,

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

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> But turning one more time:
>
[quoted text clipped - 43 lines]
>>>> Is there a VBA function or method that will return the position of
>>>> one string inside another string?  Thanks much.
Karl E. Peterson - 04 Apr 2007 22:40 GMT
> Opps,

Flip-flopper, are ya?
Signature

.NET: It's About Trust!
http://vfred.mvps.org

Jean-Guy Marcil - 04 Apr 2007 20:41 GMT
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)

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.