excel vba打开对话框导入csv

我有这个由macroslogging器生成的vba代码。 它将csv文件导入到当前的Excel表格中,并具有一些特定的列设置。 现在,csv文件的path被硬编码为“C:\ Users \ myuser \ Desktop \ logexportdata.csv”。 我怎样才能改变这一点,以便有一个对话框提示要求用户find.csv文件进行导入?

Sub Import_log() With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\Users\myuser\Desktop\logexportdata.csv", Destination:=Range( _ "$A$2")) .Name = "logexportdata" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 2 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(5, 2, 2, 2, 2, 2, 9, 9, 9, 9, 9) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub 

尝试这个:

 Sub Import_log() With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;" & getFile, Destination:=Range( _ "$A$2")) .Name = "logexportdata" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 2 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = True .TextFileSpaceDelimiter = False .TextFileColumnDataTypes = Array(5, 2, 2, 2, 2, 2, 9, 9, 9, 9, 9) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End Sub Function GetFile() As String Dim filename__path As Variant filename__path = Application.GetOpenFilename(FileFilter:="Csv (*.CSV), *.CSV", Title:="Select File To Be Opened") If filename__path = False Then Exit Function GetFile = filename__path End Function