使用VBA从csv文件中删除错误记录的最佳方法

问题描述 投票:0回答:1

以下脚本的工作原理是将错误记录添加到我的文件的异常中。

我只需要在复制后从源文件中删除坏记录的最佳方法。

Public Sub readopentextfile()
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim oFS, oFSO, oFSW
Dim stext As String
Set oFSO = CreateObject("Scripting.FileSystemObject")

'/ Open new DailySalesOrds for reading
Set oFS = oFSO.OpenTextFile("C:\DailySalesOrds.csv", ForReading)

'/ Open exception file for writing
Set oFSW = oFSO.OpenTextFile("C:\DailySalesOrderExcep.csv", ForAppending)

'/ Read each record and count commas if 21 or more add record to exception file
Do Until oFS.AtEndOfStream
         stext = oFS.ReadLine
    Do
    c = InStr(c + 1, stext, ",")
        If c <> 0 Then Count = Count + 1
    Loop Until c = 0

'Debug.Print Count
    If Count >= 21 Then
       oFSW.Write (vbNewLine & stext)
    End If
c = 0
Count = 0
Loop
End Sub
vba access-vba
1个回答
1
投票
  • 将好行写入新文件
  • 使用扩展名.bak重命名源文件
  • 将新文件重命名为原始源文件
© www.soinside.com 2019 - 2024. All rights reserved.