![]() |
Search multiple strings in one text file
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 |
Search multiple strings in one text file
What happens if you just use LineOfText (drop the 2 from the variable name)?
flaterp wrote: 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 -- Dave Peterson |
Search multiple strings in one text file
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 -- HTH, Bernie MS Excel MVP "flaterp" wrote in message ... 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 |
Search multiple strings in one text file
" Run time error 55. File already open."
"Dave Peterson" wrote: What happens if you just use LineOfText (drop the 2 from the variable name)? flaterp wrote: 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 -- Dave Peterson |
Search multiple strings in one text file
Ignore the error. I overlooked one of my "LineofText2". It works.
"flaterp" wrote: " Run time error 55. File already open." "Dave Peterson" wrote: What happens if you just use LineOfText (drop the 2 from the variable name)? flaterp wrote: 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 -- Dave Peterson |
All times are GMT +1. The time now is 12:19 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com