+ Reply to Thread
Results 1 to 4 of 4

line input problem-need to return 5 lines after a string is found

  1. #1
    festdaddy
    Guest

    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. #2
    Tom Ogilvy
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > 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. #3
    Registered User
    Join Date
    08-31-2005
    Posts
    13
    try this

    Please Login or Register  to view this content.

  4. #4
    festdaddy
    Guest

    Re: line input problem-need to return 5 lines after a string is found

    Thanks Tom!


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1