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.
我已经阅读过这篇文章:在VBA中逐行读取/解析文本文件。这篇文章告诉你如何从文本文件中读取一行。但是,我需要读一行并检查它是否包含数字,如果有,我需要删除该行并保存文本文件。
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.
我可以读取该行并检查它是否包含数字。但我不知道如何删除该行并保存文本文件。需要一些指导。
1 个解决方案
#1
3
You'll need to re-write the file, In other words:
你需要重写文件,换句话说:
- Open
input.txt
for Input - Open
output.txt
for Output - Write all the lines that don't match to
output.txt
- Delete
input.txt
- Rename
output.txt
toinput.txt
打开输入的input.txt
打开输出的output.txt
写下与output.txt不匹配的所有行
将output.txt重命名为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"
#1
3
You'll need to re-write the file, In other words:
你需要重写文件,换句话说:
- Open
input.txt
for Input - Open
output.txt
for Output - Write all the lines that don't match to
output.txt
- Delete
input.txt
- Rename
output.txt
toinput.txt
打开输入的input.txt
打开输出的output.txt
写下与output.txt不匹配的所有行
将output.txt重命名为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"