Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default line input problem-need to return 5 lines after a string is found

I've been tinkering with this for a few hours and can't seem to get it
to work correctly.
I'm using line input to go through each line looking for a string. When
the string is found, i want to return the line it came from, and the 4
lines below it. Would this be easier with an array? Or do I just have
the loop/for sequencing wrong?

sub return5lines ()

dim filename as sstring = "C:\myfile.txt"
dim strdata as string
dim texttofind as string = "my text"

Open filename For Input As #1
Do Until EOF(1)
Line Input #1, strdata
If InStr(1, strdata, texttofind) Then
i = 1
End If
For i = 1 To 5
ActiveSheet.Range("a" & i) = strdata
i = i + 1
Loop
Close #1

end sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default line input problem-need to return 5 lines after a string is found

sub return5lines ()

dim filename as sstring = "C:\myfile.txt"
dim strdata as string
dim texttofind as string = "my text"

Open filename For Input As #1
Do Until EOF(1)
Line Input #1, strdata
If InStr(1, strdata, texttofind) Then
For i = 1 To 5
ActiveSheet.Range("a" & i) = strdata
Line Input #1, strDate
next
exit do
end if
Loop
Close #1

end sub

--
Regards,
Tom Ogilvy

"festdaddy" wrote in message
oups.com...
I've been tinkering with this for a few hours and can't seem to get it
to work correctly.
I'm using line input to go through each line looking for a string. When
the string is found, i want to return the line it came from, and the 4
lines below it. Would this be easier with an array? Or do I just have
the loop/for sequencing wrong?

sub return5lines ()

dim filename as sstring = "C:\myfile.txt"
dim strdata as string
dim texttofind as string = "my text"

Open filename For Input As #1
Do Until EOF(1)
Line Input #1, strdata
If InStr(1, strdata, texttofind) Then
i = 1
End If
For i = 1 To 5
ActiveSheet.Range("a" & i) = strdata
i = i + 1
Loop
Close #1

end sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default line input problem-need to return 5 lines after a string is found


try this


Code
-------------------
sub return5lines ()

dim filename as sstring = "C:\myfile.txt"
dim texttofind as string = "my text"
dim output() as string, count as long, I as integer
I = 6
Open filename For Input As #1
Do Until EOF(1)
Line Input #1, strdata
If InStr(1, strdata, texttofind) or I < 5 Then
count = count + 1
Redim Preserve output(count)
output(count)=strdata
If InStr(1, strdata, texttofind) then
I = 1
Else
I = I + 1
End If

Loop

Close #1

Range("A1").select
Range(Selection, Selection.Offset(count, 0)).Select
Selection.Name = "OutputData"
[OutputData]=Application.Worksheetfunction.transpose(output)

end su
-------------------

--
Lauri
-----------------------------------------------------------------------
Laurin's Profile: http://www.excelforum.com/member.php...fo&userid=2683
View this thread: http://www.excelforum.com/showthread.php?threadid=49184

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default line input problem-need to return 5 lines after a string is found

Thanks Tom!

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
Problem with custom line chart, lines with two axes Dora Smith Charts and Charting in Excel 1 February 7th 07 01:43 PM
URGENT -- search in string for a value in another column, if found, return value from next column samkshah Excel Programming 4 October 3rd 05 04:13 PM
Marking all lines with same value when a second value is found Jdull Excel Discussion (Misc queries) 2 July 5th 05 08:14 PM
Search column - return row found in long text string JayL Excel Programming 1 March 5th 05 03:23 AM
Reads entire *.txt file into string opposed to a desired line by line input. ej_user Excel Programming 3 October 11th 04 07:15 PM


All times are GMT +1. The time now is 08:34 PM.

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

About Us

"It's about Microsoft Excel"