我正在使用我在网上找到的查询方法将管道分隔的文本文件导入到 Excel 电子表格中。该代码运行起来既漂亮又快速,但是,每次我运行它时,它都会创建一个奇怪的命名范围。问题是我似乎找不到使用 VBA 删除范围的有效方法,我只能使用名称管理器进行删除。这是我的代码:
Function doFileQuery(fileName As String, outSheet As String) As Boolean
Dim rootDir As String
rootDir = "W:\Development"
Dim connectionName As String
connectionName = "TEXT;" + rootDir + "\" + fileName
With Worksheets(outSheet).QueryTables.Add(Connection:=connectionName, Destination:=Worksheets(outSheet).Range("A5"))
.Name = fileName
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "|"
.Refresh BackgroundQuery:=False
End With
doFileQuery = True
End Function
这是创建的奇怪命名范围:_11r_3_Eastdata_FrankLaRoccaMonths_c_4
这是我尝试删除它的方法: ThisWorkbook.Names("_11r_3_Eastdata_FrankLaRoccaMonths_c_4").Delete
当 VB 遇到上述语句时,它给了我一个
运行时错误“1004”:
应用程序定义或对象定义错误。
任何帮助将不胜感激!