我有 100 多个以分号分隔的文件(*.asc,但打开方式与文本文件相同)要导入工作表。我可以每次选择文件并根据需要更新脚本中的文件夹。我只是想节省点击菜单的时间。
我可以成功选择文件,但它在 .Refresh BackgroundQuery 行之后给了我“错误 104 Excel 找不到刷新此外部数据范围的文本文件”。
我没有正确引用某些东西吗?
Sub Macro2input()
'
' Macro2input Macro
'
'
Dim Start1 As Range
'Dim SourceCells As Range
'Dim DestinationCells As Range
'Dim NextStart As Range
Set Start1 = ActiveCell
Dim Finfo As String
Dim vFilename As Variant
Dim fileToOpen As Variant
rootDir = "X:\Lab Tests\13-7242\Re-run Calon\1-B"
Finfo = "All Files (*.*), *.*"
'Sets the default folder for selecting a file
ChDrive "X:"
ChDir rootDir
Filename = Application.GetOpenFilename(Finfo, 1, "Select A File To Import")
vFilename = rootDir & Filename
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" + vFilename, Destination:=Start1)
.Name = Filename
.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 = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ActiveCell.Offset(0, 8).Select
End Sub