导入csv(VBA)时从资源pipe理器打开文件

我创build了一个button,可以将特定的.csv文件导入到我的Excel表格中。 但是,我想指定点击button时打开哪个文件。 所以如果按下button:Excel打开资源pipe理器,用户可以指定打开哪个文件。

With ActiveSheet.QueryTables.Add(Connection:="TEXT;Path\20150728.csv", _ Destination:=Range("$A$1")) .Name = "Tasks-Job--1g2MZtgw-Feike_15min_data-20150728" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 932 .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) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With Columns("A:A").Select Selection.Delete Shift:=xlToLeft 

我试图实现以下代码:

 Call Shell("explorer.exe" & " " & "Path", vbNormalFocus) 

但我没有设法使它正常工作。 有什么build议么?

您可以使用Excel来允许您使用FileOpen对话框select您的文件:

 With Application.FileDialog(msoFileDialogFilePicker) .Show strFileName = .SelectedItems(1) End With 

strFileName包含所选文件的完整path+文件名,您应该可以使用类似的东西

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

以下代码片段允许打开资源pipe理器并启用select文件。 但它不打开文件和系统挂起。 它可以帮助你进一步进步。

  Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _ (ByVal hwnd As Long, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As String, _ ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long Sub test() Dim Tskrfile Tskrfile = Application.GetOpenFilename() If Tskrfile <> False Then ShellExecute 0, vbNullString, Tskrfile, vbNullString, vbNullString, vbNormalFocus End If End Sub 

HTH