我创建了一个按钮,可以将特定的.csv文件导入到我的Excel工作表中。但是,我想指定单击按钮时打开的文件。因此,如果按下按钮:Excel将打开资源管理器,用户可以指定要打开的文件。
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)
但我没有设法让它正常工作。有什么建议吗?
答案 0 :(得分:1)
您可以使用Excel来允许您使用FileOpen对话框选择文件:
With Application.FileDialog(msoFileDialogFilePicker)
.Show
strFileName = .SelectedItems(1)
End With
strFileName
包含所选文件的完整路径+文件名,您应该可以使用
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & strFileName, _
Destination:=Range("$A$1"))
答案 1 :(得分:0)
以下代码片段允许打开资源管理器并启用选择文件。但它没有打开文件和系统挂起。它可以帮助您进一步发展。
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