簡體   English   中英

Excel2016上的getopenfilename VBA代碼

[英]getopenfilename VBA code on Excel2016

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM