Search multiple strings in one text file

F

flaterp

I wish to search for 2 separate strings in one file. I prefer to not follow
this sequence: (1) Open txt file (2) search for first string (3) output
result (4) close txt file (5) repeat 1-4 for second string. I would like to
perform the query without having to close and reopen the txt file. My
current program resembles what it listed below. Obviously my syntax for
"LineofText2" is incorrect, so please offer your suggestions. Is possible to
do something along the lines of "Open fName For Input As #1 and #2"? thanks
in advance.

Open fName For Input As #1
sStr = ""
sStr2 = ""
Do While Not EOF(1)
Line Input #1, LineofText
If (InStr(1, LineofText, "Reboot", vbTextCompare)) Then
sStr = Left(LineofText, 12)
End If
If (InStr(1, LineofText2, "Yes", vbTextCompare)) Then
sStr2 = Mid(LineofText2, 12, 3)
End If
Loop
Cells(iVar, "A").Value = sStr
Cells(iVar, "B").Value = sStr2
Close #1
 
D

Dave Peterson

What happens if you just use LineOfText (drop the 2 from the variable name)?
 
B

Bernie Deitrick

Simply search the same string twice:


Sub TryNow()
Dim fName As String
Dim LineofText As String
Dim sStr As String
Dim sStr2 As String
Dim iVar As Long
Dim Changed As Boolean

fName = "Whatever.prn"

Open fName For Input As #1
sStr = ""
sStr2 = ""
iVar = 1
Do While Not EOF(1)
Line Input #1, LineofText
Changed = False
If InStr(1, LineofText, "Reboot", vbTextCompare) > 0 Then
sStr = Left(LineofText, 12)
Cells(iVar, "A").Value = sStr
Changed = True
End If
If InStr(1, LineofText, "Yes", vbTextCompare) > 0 Then
sStr2 = Mid(LineofText, 12, 3)
Cells(iVar, "B").Value = sStr2
Changed = True
End If
If Changed Then iVar = iVar + 1
Loop
Close #1
End Sub
 

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