所以我有这段代码可以将.csv文件中的数据加载到Excel工作表中。我录制了宏,但是每次执行宏时如何让用户选择自定义文件?
Range("B11").Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Users\admin\Desktop\VAT\te\VAT_SALES_201801.csv", Destination:=Range _
("$B$11"))
.Name = "VAT_SALES_201801"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 3
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 9, 1, 9, 1, 1, 1, 1, 9, 9, 9, 9, 9)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
我不希望对文件部分进行硬编码。
答案 0 :(得分:2)
Sub Button1_Click()
fileToOpen = Application _
.GetOpenFilename("csv Files (*.csv), *.csv")
If fileToOpen <> False Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & FileToOpen, Destination:=Range _
("$B$11"))
.Name = "VAT_SALES_201801"
'...
'...
End If
End Sub