我有一个 VBA 脚本来导入 txt 文件。它在 Excel 2013 上运行良好。在 excel 2016 上,getopenfilename 不再支持参数(excel 崩溃)。它在删除 getopenfilename() 的所有参数时起作用
任何想法 ?
Sub Import_TXT()
On Error GoTo Err1
With Sheets("Sheet2").QueryTables.Add(Connection:= _
"TEXT;" & GetTXT, Destination:=Sheets("Sheet2").Range("A1"))
.Name = "logexportdata"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = False
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.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)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Exit Sub
Err1:
MsgBox "Data not imported. Error: " & Err.Number & vbCrLf & Err.Description
End Sub
Function GetTXT() As String
Dim filename__path As Variant
' Get the filename
filename__path = Application.GetOpenFilename(FileFilter:="TXT (*.txt), *.txt", Title:="Select txt file")
If filename__path = False Then Exit Function
GetTXT = filename__path
End Function