使用VBA将多个CSV文件导入Excel中的单个工作表(当前只能执行1项操作)

使用VBA将多个CSV文件导入Excel中的单个工作表(当前只能执行1项操作),excel,vba,Excel,Vba,我设计了一个支持VBA的工作簿,允许用户选择一个.csv文件(这是从另一个系统导出的客户文件),然后它基本上会根据各种用户定义的标准对其进行处理,以生成多个不同的用户组 一切都很顺利。但是,它一次只能处理一个.csv文件 我目前采用的方法基本上是将所选CSV文件的内容导入到一个新的工作表中,然后我只需查询该数据并对其执行我需要的操作。然而,由于很长一段时间没有使用VBA,我不知道如何在我已经编写的代码的基础上进行构建,以允许选择多个CSV文件 With Application.FileD

我设计了一个支持VBA的工作簿,允许用户选择一个.csv文件(这是从另一个系统导出的客户文件),然后它基本上会根据各种用户定义的标准对其进行处理,以生成多个不同的用户组

一切都很顺利。但是,它一次只能处理一个.csv文件

我目前采用的方法基本上是将所选CSV文件的内容导入到一个新的工作表中,然后我只需查询该数据并对其执行我需要的操作。然而,由于很长一段时间没有使用VBA,我不知道如何在我已经编写的代码的基础上进行构建,以允许选择多个CSV文件

    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        'We only want to allow CSV files as this is what the ADT comes in
        .Filters.Add "ADT CSV Files", "*.csv", 1
        'Show the dialog box
        .Show

    'Error check in case user cancels dialog box to prevent type-mismatch error

    If (.SelectedItems.Count = 0) Then

        Range("C19").Value = "File selection aborted."
    Else
        'Store in fullpath variable
        Range("C19").Value = "Processing..."
        fullpath = .SelectedItems.Item(1)
    End If
    End With

    'A final check to make sure that the user hasn't done anything odd and somehow selected an invalid file format

    If InStr(fullpath, ".csv") = 0 Then
        Exit Sub
    End If

    Range("J26").Value = "Source File:"
    Range("J27").Value = fullpath

    'Now we grab the data from the file and import it into a new sheet within workbook

    Set Ws = ThisWorkbook.Sheets.Add
    Ws.Name = "ADT Data"

    'The ADT seems to be using fairly standard formatting conditions, so the following should surfice

         With Ws.QueryTables.Add(Connection:= _
        "TEXT;" & fullpath, Destination:=Ws.Range("$A$1"))

        .Name = "ADT Data"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileCommaDelimiter = True
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False

    End With

    'Now we trigger our main triage processes

    Call Extract

我假设我需要将所选文件添加到数组中,然后循环遍历它们,但根据我的VBA知识,我不确定如何实现。

您将此选项设置为false

.AllowMultiSelect=False

我将创建一个对象来引用FileDialog


'将变量声明为FileDialog对象,并将其设置为:
将fd设置为文件对话框
'将FileDialog对象创建为文件选择器对话框。
Set fd=Application.FileDialog(msoFileDialogFilePicker)

然后可以迭代fd对象的SelectedItems集合。

对于中的每个VRT选择编辑项。选择编辑项

然后对选定的每个文件执行操作

AllowMultiSelect文档提供了一些很好的信息


希望这有帮助。

设置
.AllowMultiSelect=True
,然后迭代
。选择editems
。查看如何使用
。选择编辑项。项(1)
?相反,对i=1执行
到.SelectedItems.Count
,然后使用
引用每个选定的.csv。SelectedItems.Item(i)
。allowmultiselect=true
,存储每个文件的名称,然后循环使用这些文件名,如.SelectedItems中的每个项目的
,并附加到工作簿的最后一行+1