Replacing multiple instances of text file lines

frateg8r

Board Regular
Joined
Mar 2, 2005
Messages
221
Office Version
  1. 365
Platform
  1. Windows
I have text files that may have one, or multiple, instances of the following line groupings:
VBA Code:
PG27FANU1022356                                                                 
PG2803110701                                                                   
PG30A0331202412002   2809

I have the following procedure (part of a larger module running a number of different validations in the text file) which looks to check whether the date in the PG30 record is correct or not and change it if not. The procedure works fine if there is only a single instance of the PG30 record; however, if there are multiple instances, none of them end up being changed.

VBA Code:
            Set tso = ofs.OpenTextFile(sFilePath & sFileName)
            Do While Not tso.AtEndOfStream
            sLine = tso.ReadLine
            Dim sETA As String
            Dim sReplaceLine As String
            sETA = "04092024"
    
            If Left(sLine, 4) = "PG30" Then
                If sETA <> Mid(sLine, 6, 8) Then
                    sReplaceLine = Left(sLine, 5) & sETA & Right(sLine, 67)
                    Open sFilePath & sFileName For Output As #2
                    Print #2, Replace(Trim(strFileText), sLine, sReplaceLine);
                    Close #2
                    Print #1, "PG30 ETA Updated"
                Else
                    Print #1, "PG30 ETA Verified"
                End If
            End If
            Loop

I suspect the problem stems from the replacement text file being lost when the loop starts for the second instance of the PG30 record, since the original file is still open. I did try adding "tso.close" after the "Close #2" line, but that did not affect the outcome. If I killed the sub after that, then the replacement file was in place with the first instance corrected, but of course, not the second one.

How can I exit the loop and restart from the top (the "Set tso" line), so as to continue processing from the second instance of the PG30 record?
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
It looks to me like you are trying to modify the same input file that you are reading, while you are still reading it - and that is a recipe for trouble. Instead use two files. One is the input file for reading that you don't modify, the other is the output file for writing that you do.
Not tested.
VBA Code:
    Dim sETA As String          'don't declare variables inside a loop
    Dim sReplaceLine As String
    
    Set tso = ofs.OpenTextFile(sFilePath & sFileName)
    
    Open sFilePath & "Output.txt" For Output Access Write As #2
    
    Do While Not tso.AtEndOfStream
        sLine = tso.ReadLine
        
        sETA = "04092024"
        
        If Left(sLine, 4) = "PG30" Then
            If sETA <> Mid(sLine, 6, 8) Then
                sReplaceLine = Left(sLine, 5) & sETA & Right(sLine, 67)
                sLine = sReplaceLine
                Print #1, "PG30 ETA Updated"
            End If
        Else
            Print #1, "PG30 ETA Verified"
        End If
        Print #2, sLine
    Loop
    Close #2

Once the processing is complete, add code to (if you desire) to delete the original file and replace it with the new updated file.
 
Upvote 0
Solution
It looks to me like you are trying to modify the same input file that you are reading, while you are still reading it - and that is a recipe for trouble. Instead use two files. One is the input file for reading that you don't modify, the other is the output file for writing that you do.
Not tested.
VBA Code:
    Dim sETA As String          'don't declare variables inside a loop
    Dim sReplaceLine As String
   
    Set tso = ofs.OpenTextFile(sFilePath & sFileName)
   
    Open sFilePath & "Output.txt" For Output Access Write As #2
   
    Do While Not tso.AtEndOfStream
        sLine = tso.ReadLine
       
        sETA = "04092024"
       
        If Left(sLine, 4) = "PG30" Then
            If sETA <> Mid(sLine, 6, 8) Then
                sReplaceLine = Left(sLine, 5) & sETA & Right(sLine, 67)
                sLine = sReplaceLine
                Print #1, "PG30 ETA Updated"
            End If
        Else
            Print #1, "PG30 ETA Verified"
        End If
        Print #2, sLine
    Loop
    Close #2

Once the processing is complete, add code to (if you desire) to delete the original file and replace it with the new updated file.
Thank you! In retrospect, that's an obvious solution - guess I just spent too much time inside this code and needed a fresh set of eyes. Also taking your advice on declaring variables outside a loop - thanks for that.
 
Upvote 0

Forum statistics

Threads
1,223,778
Messages
6,174,482
Members
452,566
Latest member
Bonnie_bb

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top