间歇文本导入错误(错误9)

我不确定这是否适合这个问题,但是这个问题有点出于我的知识基础。

我的问题是,我有一段代码,只是导入一个文本文件,并使用空格分隔符。 我已经在terminal上多次使用这个代码,并且工作正常。 但是,当我在一个不同的terminal上运行它时,我得到一个错误,其他时间很好。

我的代码是:

Dim Ret

Ret = Application.GetOpenFilename("Text Files (*.txt), *.txt") If Ret <> False Then With Sheets("Primary").QueryTables.Add(Connection:= _ "TEXT;" & Ret, Destination:=Workbooks("Importer").Sheets("Primary").Range("$A$1")) .Name = "Sample" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = True .TextFileTabDelimiter = True .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = True .TextFileColumnDataTypes = Array(xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat, xlTextFormat) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With End If 

我运行此代码时出现“运行时错误9:下标超出范围”。 debugging菜单select此代码的问题:

使用表格(“Primary”)。QueryTables.Add(Connection:= _“TEXT;”&Ret,Destination:= Workbooks(“Importer”)。Sheets(“Primary”)。Range(“$ A $ 1”))

我不明白是什么原因导致这只在某些电脑terminal。 据我所知,我们所有的Excel程序设置相同。

任何帮助将不胜感激。

“运行时错误9:下标超出范围”是由无效的索引/名称给一个数组引起的..说代码Sheets("Primary")但没有名为主..

具体到这种情况下,与给定的信息,我怀疑是因为工作簿的名称已经发生了错误已经改变…所以代码Workbooks("Importer")尝试看工作簿名为import商和失败..