Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default 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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default 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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default 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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default 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





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
search for multiple strings Lea from CA[_2_] Excel Discussion (Misc queries) 4 October 16th 09 10:20 PM
How do I filter or search mulitple for text strings? MikeD Excel Discussion (Misc queries) 2 October 29th 08 01:07 PM
Search for multiple strings in a list (w/in 1 cell) w/ Advanced fi Maher Excel Discussion (Misc queries) 5 July 7th 08 06:02 PM
Search multiple strings in each cell Dave Peterson Excel Programming 7 December 18th 06 06:05 PM
Search multiple strings Difficult to figure out Jeff Excel Programming 2 November 23rd 06 04:18 AM


All times are GMT +1. The time now is 02:56 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"