1

I have read this post: Read/Parse text file line by line in VBA. This post tells you how to read a line from a text file. However, I need to read a line and check whether it contains a number and if it does, I need to delete the line and save the text file.

While Not EOF(FileNum)
    Line Input #FileNum, DataLine
    If FindValue(DataLine) Then
        'Stuck here.
    End If
Wend

End Sub

Function FindValue(ByVal DataLine As Variant) As Boolean
    For Index = 0 To NoOfLiquidatedDeals - 1
        pos = InStr(DataLine, NoOfLiquidatedDealsArray(Index))
        If pos > 0 Then
            FindValue = True
        End If
    Next
End Function

I can read the line and check whether it contains a number. But I am not sure how to delete the line and save the text file. Need some guidance on this.

Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276

1 Answers1

7

You'll need to re-write the file, In other words:

  1. Open input.txt for Input
  2. Open output.txt for Output
  3. Write all the lines that don't match to output.txt
  4. Delete input.txt
  5. Rename output.txt to input.txt

And in code:

Open "input.txt" For Input as #1
Open "output.txt" For Output as #2
While Not EOF(#1)
    Input #1, DataLine
    If Not FindValue(DataLine) Then
        Print #2,DataLine
    End If
Wend
Close #2
Close #1
Kill "input.txt"
Name "output.txt" As "input.txt"
Uri Goren
  • 13,386
  • 6
  • 58
  • 110