【问题标题】:How to delete a line in a txt file if number exists in VBA如果VBA中存在数字,如何删除txt文件中的一行
【发布时间】:2015-07-14 20:56:32
【问题描述】:

我已阅读这篇文章:Read/Parse text file line by line in 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

我可以读取该行并检查它是否包含数字。但我不确定如何删除该行并保存文本文件。在这方面需要一些指导。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您需要重新编写文件, 换句话说:

    1. 打开input.txt 输入
    2. 打开output.txt 输出
    3. 将所有不匹配的行写入output.txt
    4. 删除input.txt
    5. output.txt 重命名为input.txt

    在代码中:

    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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多