+ Reply to Thread
Results 1 to 4 of 4

Read Next Line for Check only

Hybrid View

  1. #1
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Read Next Line for Check only

    Hello Friends,

    I am reading text file and i just want to check what next line contain
    but cursor location should not change

    e.g.
    currently i am reading Line No. 5 (Using ReadLine Function)
    now i want to check what Line No. 6 contain
    after checking i want to read Line No. 6 (but my cursor location goes on 7th Line)

    In Short is it possible to move the current cursor (ReadLine) to Previous line
    In short i want to read a single Line twice (Move cursor Backward and forward)

    Here is my Code

    Dim ReadLine
    Dim FN As Integer
    Dim FileLoc As String
    FileLoc = "D:\Transfer\Sample.txt"
    FN = FreeFile
    Open FileLoc For Input As #FN    ' Open file.
    Do While Not EOF(FN)    ' Loop until end of file.
         
        Line Input #1, ReadLine    ' Get one Line.
        Debug.Print ReadLine     ' Print to the Immediate window.
    
    Loop
    Close #FN    ' Close file.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,660

    Re: Read Next Line for Check only

    Quote Originally Posted by mohan.r1980 View Post
    In Short is it possible to move the current cursor (ReadLine) to Previous line
    In short i want to read a single Line twice (Move cursor Backward and forward)
    You cannot move the line pointer backwards in Open file For Input mode. It reads sequentially and forward.

    You could read each line and keep track of the previous line with another variable. Something like this...

    Sub foo()
    Dim ReadLine1 As String, ReadLine2 As String
    Dim FN As Integer
    Dim FileLoc As String
    FileLoc = "D:\Transfer\Sample.txt"
    FN = FreeFile
    Open FileLoc For Input As #FN    ' Open file.
    Do While Not EOF(FN)    ' Loop until end of file.
         
        ReadLine1 = ReadLine2
        Line Input #1, ReadLine2    ' Get one Line.
        Debug.Print ReadLine1     ' Print previous line to the Immediate window.
        Debug.Print ReadLine2     ' Print to the Immediate window.
    
    Loop
    Close #FN    ' Close file.
    End Sub
    Alternatively, you could Read the first line, Store the pointer position, Read the next line, close and reopen the same file. Then read up to the stored pointer position.

    Also, there are the two other modes; Open for Random Access and Binary. Both have advantages and disadvantages and are a little more complicated. Do a web search for something like VB File Access "Random Access" Binary mode for several useful step-by-step guides. This one looks helpful.
    FILE HANDLING USING VISUAL BASIC 6.0 PART I
    Last edited by AlphaFrog; 11-02-2012 at 12:04 PM.

  3. #3
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: Read Next Line for Check only

    hi AlphFrog,

    i already tried this solution

  4. #4
    Registered User
    Join Date
    10-29-2012
    Location
    Bedfordshire, England
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Read Next Line for Check only

    Hi Mohan.

    You could try reading the lines of the file into an array first.
    Then you could access any line at any time.

    Dim ReadLine
    Dim FN, LineCount As Integer
    Dim FileLoc As String
    Dim FileArray()
    FileLoc = "D:\Transfer\Sample.txt"
    FN = FreeFile
    LineCount = 0
    
    'First read the lines in the file into an array
    Open FileLoc For Input As #FN               'Open file.
        Line Input #1, ReadLine                 'Get one Line.
        LineCount = LineCount + 1               'Increment a counter
        ReDim Preserve FileArray(LineCount)     're-dimension the array so that it can add the new line
        FileArray(LineCount) = ReadLine         'and add the line to the array
    Close #FN
    
    'Now read the array instead of the file and you can move backwards and forwards through the lines
    For a = 1 To UBound(FileArray)
        Debug.Print FileArray(a)                ' Print to the Immediate window.
        If a > 1 Then
            Debug.Print FileArray(a - 1)        ' Prints the previous line to the Immediate window.
        End If
        If a < UBound(FileArray) Then
            Debug.Print FileArray(a + 1)        ' Prints the next line to the Immediate window.
        End If
    Next

+ 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