macros提示用户selectCSV文件导入工作簿中的现有工作表

我正在运行一个macros,它会自动将csv文件导入到我的工作簿中的特定工作表中。 但是,我想通过让用户select要导入的文件而不是让macros自动获取csv文件来增加更大的灵活性,因为命名和目录一样可以更改。 我是VBA新手,一直试图更好地理解MsoFileDialogType和GetOpenFilename,但是很难将概念/实现理解为我的代码。

我最终想要的是让用户单击工作簿前端的button。 提示消息以select要导入的第一个csv文件。 此csv文件将被导入到工作簿temp1中的一个预先命名的工作表中。 但是,由于数据文件成对出现,我希望用户能够在第一个文件之后select下一个csv文件到temp2中。

我目前所拥有的是:

Worksheets.Add ActiveSheet.Name = "temp1" With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;MAC Directory path here" _ , Destination:=Range("A1")) .Name = "temp 1 03.02.12" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .TextFilePromptOnRefresh = False .TextFilePlatform = xlMacintosh .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 1, 1, 1) .Refresh BackgroundQuery:=False .UseListObject = False End With ActiveSheet.Move after:=Worksheets(Worksheets.Count) 

谢谢。

也许在这些线上的东西。

 Sub GetCSVList() Dim dlgOpen As FileDialog Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker) With dlgOpen .AllowMultiSelect = True ''Start in .InitialFileName = "Z:\docs\" .Show End With For Each fname In dlgOpen.SelectedItems ImportCSV fname Next End Sub Sub ImportCSV(fname) Set ws = Worksheets.Add(after:=Worksheets(Worksheets.Count)) ws.Name = "temp" & Worksheets.Count + 1 With ws.QueryTables.Add( _ Connection:="TEXT;" & fname, _ Destination:=Range("A1")) .Name = "Temp" & Worksheets.Count + 1 .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .TextFilePromptOnRefresh = False .TextFilePlatform = xlMacintosh .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .Refresh BackgroundQuery:=False '.UseListObject = False End With End Sub