使用 VBA 同时格式化多个 .csv 文件

Format multiple .csv files simultaneusly using VBA

提问人:Cyron2509 提问时间:11/10/2023 最后编辑:FunThomasCyron2509 更新时间:11/11/2023 访问量:52

问:

我正在尝试运行一个代码,该代码一次格式化特定文件夹中的多个 .csv 文件。在原始数据表中,包含值的行是间隔开的。

我想删除空行并将单元格中包含的数值提取到另一个相邻单元格。

我尝试使用的代码适用于单个工作表,但一旦我无法将其调整为在多个文件上使用。

Sub RunOnAllFilesInFolder()
    Dim folderName As String, eApp As Excel.Application, fileName As String
    Dim wb As Workbook, ws As Worksheet, currWs As Worksheet, currWb As Workbook
    Dim fDialog As Object: Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    Set currWb = ActiveWorkbook: Set currWs = ActiveSheet
 
   folderName = "C:\xxxx\test\"
    
    
    'Create a separate Excel process that is invisibile
    Set eApp = New Excel.Application:  eApp.Visible = False
    
    'Search for all files in folder [replace *.* with your pattern e.g. *.xlsx]
    fileName = Dir(folderName & "\*.csv")
    Do While Len(fileName) > 0
        'Update status bar to indicate progress
        Application.StatusBar = "Processing " & folderName & "\" & fileName
 
 
        Set wb = eApp.Workbooks.Open(folderName & "\" & fileName)
        '...
        
        
        
    Dim s As String, i As Long, L As Long, c As String, temp As String, d As String, p As String, v As String
    
    Range("A11").Select
    Selection.Cut
    Range("A10").Select
    ActiveSheet.Paste
    Range("A13").Select
    Selection.Cut
    Range("A11").Select
    ActiveSheet.Paste
    Range("A15").Select
    Selection.Cut
    Range("A12").Select
    ActiveSheet.Paste
    
    s = [A9]
    L = Len(s)
    temp = ""
    For i = 1 To L
        c = Mid(s, i, 1)
        If c Like "[0-9]" Then
            temp = temp & c
        Else
            temp = temp & " "
        End If
    Next i

    temp = "'" & Application.WorksheetFunction.Trim(temp)
    temp = Replace(temp, " ", ",")

    [D9] = temp
    
    
    d = [A10]
    L = Len(s)
    temp = ""
    For i = 1 To L
        c = Mid(d, i, 1)
        If c Like "[0-9]" Then
            temp = temp & c
        Else
            temp = temp & " "
        End If
    Next i

    temp = "'" & Application.WorksheetFunction.Trim(temp)
    temp = Replace(temp, " ", ",")

    [D10] = temp
    
    
    
    p = [A11]
    L = Len(s)
    temp = ""
    For i = 1 To L
        c = Mid(p, i, 1)
        If c Like "[0-9]" Then
            temp = temp & c
        Else
            temp = temp & " "
        End If
    Next i

    temp = "'" & Application.WorksheetFunction.Trim(temp)
    temp = Replace(temp, " ", ",")

    [D11] = temp
    
    
    v = [A12]
    L = Len(s)
    temp = ""
    For i = 1 To L
        c = Mid(v, i, 1)
        If c Like "[0-9]" Then
            temp = temp & c
        Else
            temp = temp & " "
        End If
    Next i

    temp = "'" & Application.WorksheetFunction.Trim(temp)
    temp = Replace(temp, " ", ",")

    [D12] = temp
    
    
    
    
        '...
       wb.Close SaveChanges:=True 'Close opened worbook w/o saving, change as needed
       Debug.Print "Processed " & amp; folderName & amp; "\" & amp; fileName
       fileName = Dir()
       
       
    Loop
    eApp.Quit
    Set eApp = Nothing
    'Clear statusbar and notify of macro completion
    Application.StatusBar = ""
    MsgBox "Completed executing macro on all workbooks"
End Sub

我尝试使用我在网上找到的一些代码,它将始终运行而不会出现错误消息,但最终,这些文件似乎不受影响。即使在拆分任务后(例如,只是在不提取数值的情况下移动数据),它仍然无法正常工作。

Excel VBA CSV

评论

0赞 Tim Williams 11/11/2023
之前/之后的屏幕截图在这里会有所帮助 - 从代码中猜测布局有点费力
0赞 CDP1802 11/11/2023
你有 A10、A11、A12,但应该是 , , .csv 文件在单独的 Excel 进程 (eApp) 中打开,因此不会是 的活动表。L = Len(s)Len(d)Len(p)Len(s)ActiveSheet.Paste
0赞 Black cat 11/11/2023
分配 eApp.Visible=True,并用它测试代码。

答:

2赞 CDP1802 11/11/2023 #1
Option Explicit

Sub RunOnAllFilesInFolder()

    Dim wb As Workbook, cel As Range
    Dim folderName As String, fileName As String
    Dim temp As String, sep As String
    Dim regex As Object, m As Object
    Dim n As Long, i As Long, t0 As Single: t0 = Timer
    
    folderName = "C:\temp\SO\77460667" ' "C:\xxxx\test\"
    If Right(folderName, 1) <> "\" Then folderName = folderName & "\"
    
    ' pattern for extracting numbers
    Set regex = CreateObject("vbscript.regexp")
    With regex
        .Global = True
        .MultiLine = False
        .IgnoreCase = True
        .Pattern = "([0-9]+)"
    End With
    
    ' Search for all csv files in folder
    fileName = Dir(folderName & "*.csv")
    Application.ScreenUpdating = False
    Do While Len(fileName) > 0
    
        'Update status bar to indicate progress
        Application.StatusBar = "Processing " & folderName & fileName
        Set wb = Workbooks.Open(folderName & fileName)
        
        ' process sheet
        With wb.Sheets(1)
            .Range("A10") = .Range("A11")
            .Range("A11") = .Range("A13")
            .Range("A12") = .Range("A15")
            .Range("A13,A15").ClearContents
            
             ' extract numbers from A to D
            For Each cel In .Range("A9:A12")
                temp = ""
                sep = ""
                Set m = regex.Execute(cel)
                For i = 1 To m.Count
                    temp = temp & sep & m(i - 1)
                    sep = ", "
                Next
                cel.Offset(, 3) = temp
            Next
        End With
        
        ' close and get next
        wb.Close SaveChanges:=True 'Close opened workbook saving changes
        fileName = Dir()
        n = n + 1
    Loop
    Application.ScreenUpdating = True
    
    'Clear statusbar and notify of macro completion
    Application.StatusBar = ""
    MsgBox n & " csv files processed", vbInformation, Format(Timer - t0, "0.0 secs")
End Sub