Consulting

Results 1 to 4 of 4

Thread: Reading in a Text File

  1. #1

    Reading in a Text File

    I can read a text file with the following code, but the issue is that "RowEnd" must be known before hand. Is there a way to determine the textline where "This is the stop text" is and then feed that to RowEnd? I basically want it to stop reading in a certain point in the text file that is where a given textline occurs, not at a fixed value.


    Do Until textline = "Start Line"
        Line Input #1, textline
    Loop
    
    For j = RowStart To RowEnd
        Line Input #1, textline
        newline = Split(textline, ",")
        Cells(j, 1) = newline(0)
        Cells(j, 2) = newline(1)
        Cells(j, 3) = newline(2)
        Cells(j, 4) = newline(3)
    
    Next j

  2. #2
    VBAX Regular
    Joined
    Jan 2018
    Location
    The Netherlands
    Posts
    45
    Location
    Please show the complete code, perhaps wit worksheet

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
      open "G\OF\example.txt" for input as #1
         msgbox split(Input(LOF(1),#1),"This is the stop Text")(0)
      close
    End Sub

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    You can try something like this - it's not tested, and can be made more elegant, but this seemed the simplest to start with


    Option Explicit
    Sub test()
        Dim iFile As Long, j As Long
        Dim bStart As Boolean
        Dim textline As String
        Dim newline As Variant
        
        iFile = FreeFile
        bStart = False
        
        Do While Not EOF(iFile)
            j = j + 1
            Line Input #1, textline
            
            If textline = "Start line" Then
                bStart = True
                GoTo Nextline
            
            ElseIf textline = "This is the stop text" Then
                Exit Do
            
            ElseIf bStart Then
                newline = Split(textline, ",")
                Cells(j, 1) = newline(0)
                Cells(j, 2) = newline(1)
                Cells(j, 3) = newline(2)
                Cells(j, 4) = newline(3)
            End If
            
    Nextline:
        Loop
        Close #iFile
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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