excel - Excel 2016 上的 getopenfilename VBA 代码

标签 excel crash excel-2016 getopenfilename vba

我有一个 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

最佳答案

我找到的解决方案是替换为

With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Text file", "*.txt", 1
.FilterIndex = 1
.Title = "Select txt file"
.Show
filename__path = .SelectedItems(1)
End With

关于excel - Excel 2016 上的 getopenfilename VBA 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47352140/

相关文章:

excel - 选中复选框时写入复选框旁边的单元格

excel - Workbooks.OpenText 无法正确解析 csv 文件 Excel 2016

excel - VBA 如何强制用户使用 Excel 的内置范围选择来选择范围?

windows-phone-8.1 - 从 toast 通知启动时,WP8.1 WinJS 应用程序崩溃

vba - 如何使用 VBA 从 Excel 中的单元格返回所有格式?

将应用分发到应用商店时 Xcode 崩溃

crash - 即使使用较小的数据集后,如何解决使用可用RAM后Google Colab中 session 崩溃的问题

excel - 如何生成常规 Excel 公式作为查询结果?

Python:xlrd 从 float 中识别日期

Excel VBA 在数字字符串中添加零