Finding a string between two words
Hello all,
I would like to be able to find out what is in the textbox between the words QQQ1 and QQQ2, however I am not sure how to do this. The code I tried to do it with is...
VB Code:
Public Sub TrimString(TheString As String, KeepAfter As String, KeepBefore As String)
Dim posA As Long
Dim posB As Long
posA = 1
posB = 1
Dim incTill As Long
incTill = Len(TheString) - Len(KeepAfter)
Debug.Print incTill & " is intill"
For i = 1 To incTill
If Mid(TheString, posA, Len(KeepAfter)) = KeepAfter Then
Debug.Print posA
GoTo FoundKeepAfter
posA = i
End If
Next
TheString = "Cannot find KeepAfter"
Exit Sub
FoundKeepAfter:
incTill = Len(TheString) - Len(KeepBefore)
For j = 1 To incTill
If Mid(TheString, posB, Len(KeepBefore)) = KeepBefore Then
GoTo FoundKeepBefore
posB = j
End If
Next
TheString = "Cannot find KeepBefore"
Exit Sub
FoundKeepBefore:
TheString = Mid(TheString, posA, posA - posB)
DoEvents
Text1.Text = TheString
End Sub
Private Sub Command1_Click()
TrimString Text1.Text, "QQ1", "QQQ2"
End Sub
But it doesnt work :( Is there some good way to do it?
Thanks