将多个 CSV 导入到具有 CSV 文件名的单个 Excel 中到新列中

Importing multiple CSV to a single Excel with CSV file name into a new column

提问人:Amy 提问时间:10/16/2023 最后编辑:Shai RadoAmy 更新时间:10/21/2023 访问量:89

问:

我正在尝试将多个 .csv 文件导入到单个 Excel 工作表中。我希望 .csv 文件名在导入过程中每行都是一个单独的列。

我的代码如下:

Sub Combinecsvs()

Dim FolderPath As String
Dim FileName As String
Dim WbResult As Workbook

FolderPath = ""
If FolderPath Like "*[!\/]" Then
    FolderPath = FolderPath & "/"
End If

FileName = Dir(FolderPath & "*.csv")
Set WbResult = ActiveWorkbook

Application.DisplayAlerts = False
Application.ScreenUpdating = False

Do While FileName <> vbNullString
    Set WB = Workbooks.Open(FolderName & FileName)
    WB.ActiveSheet.UsedRange.Copy WB.ActiveSheet.UsedRange.Rows(WbResult.ActiveSheet.UsedRange.Rows.Count).Offset(1).Resize(1)
    WB.Close False
    FileName = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
  
Excel VBA CSV

评论

2赞 Ken White 10/16/2023
你没有解释你的代码遇到的任何问题,也没有问过任何类型的问题。你刚刚告诉我们你想做什么。你是在请求许可吗?
0赞 Cem Polat 10/16/2023
还可以考虑使用 Power Query 将目录中的 csv 文件作为数据源读取。

答:

0赞 taller 10/16/2023 #1
  • CSV 文件名位于 A 列中。
  • 导入的数据从 B 列开始。
Option Explicit
Sub Combinecsvs()
    Dim FolderPath As String
    Dim FileName As String
    Dim WbResult As Workbook
    Dim WB As Workbook
    FolderPath = "D:\Temp\csv"
    If FolderPath Like "*[!\/]" Then
        FolderPath = FolderPath & "\"
    End If
    FileName = Dir(FolderPath & "*.csv")
    Set WbResult = ActiveWorkbook
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Dim c As Range
    Do While FileName <> vbNullString
        Set WB = Workbooks.Open(FolderPath & FileName)
        With WbResult.ActiveSheet
            If Len(.Cells(1, 1)) = 0 Then
                Set c = .Cells(1, 1)
            Else
                Set c = .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
            End If
            c.Value = FileName
            Set c = .Cells(1, .Columns.Count).End(xlToLeft).Offset(0, 1)
            WB.ActiveSheet.UsedRange.Copy c
        End With
        WB.Close False
        FileName = Dir()
    Loop
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

enter image description here

Microsoft 文档:

Range.End 属性 (Excel)


更新:

问题:我可以检查一下我是否允许用户选择导入 csv 文件的文件夹,我怎样才能在同一代码中做?

Option Explicit
Sub Combinecsvs()
    Dim FolderPath As String
    Dim FileName As String
    Dim WbResult As Workbook
    Dim WB As Workbook
    Dim dialog As FileDialog
    Set dialog = Application.FileDialog(msoFileDialogFolderPicker)
    With dialog
        .Title = "Select a Folder"
        If .Show = -1 Then
            FolderPath = .SelectedItems(1) & "\"
        Else
            MsgBox "You cancelled the operation."
            Exit Sub
        End If
    End With
    FileName = Dir(FolderPath & "*.csv")
    Set WbResult = ActiveWorkbook
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Dim c As Range
    Do While FileName <> vbNullString
        Set WB = Workbooks.Open(FolderPath & FileName)
        With WbResult.ActiveSheet
            If Len(.Cells(1, 1)) = 0 Then
                Set c = .Cells(1, 1)
            Else
                Set c = .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
            End If
            c.Value = FileName
            Set c = .Cells(1, .Columns.Count).End(xlToLeft).Offset(0, 1)
            WB.ActiveSheet.UsedRange.Copy c
        End With
        WB.Close False
        FileName = Dir()
    Loop
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

评论

0赞 Amy 10/21/2023
谢谢,这很有效。我可以检查我是否要允许用户选择导入 csv 文件的文件夹,我该如何在相同的代码中执行操作?
0赞 taller 10/21/2023
当然,请参阅更新后的代码。
1赞 CDP1802 10/16/2023 #2

在导入的每一行的 A 列中添加文件名。

Option Explicit

Sub Combinecsvs()

    Dim WbResult As Workbook, rngCSV As Range, rng As Range
    Dim wbCSV As Workbook, folderpath As String
    Dim FileName As String, n As Long
    
    folderpath = "C:\temp\so\csv"
    If folderpath Like "*[!\/]" Then
        folderpath = folderpath & "/"
    End If
    
    FileName = Dir(folderpath & "*.csv")
    Set WbResult = ActiveWorkbook
    
    Application.ScreenUpdating = False
    With WbResult.Sheets(1)
        Do While FileName <> vbNullString
            ' source
            Set wbCSV = Workbooks.Open(folderpath & FileName, ReadOnly:=True)
            Set rngCSV = wbCSV.Sheets(1).UsedRange
            
            ' target
            Set rng = .Cells(.Rows.Count, "A").End(xlUp).Offset(1)
            rngCSV.Copy rng.Offset(, 1)
            rng.Resize(rngCSV.Rows.Count) = FileName
            wbCSV.Close False
            n = n + 1
            
            FileName = Dir()
        Loop
    End With
    Application.ScreenUpdating = True
    
    MsgBox n & " csv files imported", vbInformation

End Sub