try this
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 sub
Bookmarks