2016-09-26 17 views
1

我不確定這是否是問題的正確位置,但這有點超出我的知識基礎。間歇文本導入錯誤(錯誤9)

我的問題是我有一段代碼只是簡單地導入一個文本文檔並使用空格分隔符。我在終端上多次使用了該代碼,並且它可以正常工作。但是,當我在一個不同的終端上運行它時,我得到了一個錯誤,其他時候它很好。

我的代碼是:

昏暗的懲戒

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:下標越界」當我運行這段代碼。調試菜單選擇用於該問題的代碼:

隨着表( 「主」)QueryTables.Add(連接:= _ 「TEXT;」 & RET,目的地:。=工作簿( 「供應商」)表( 「Primary」)。範圍(「$ A $ 1」))

我不明白是什麼導致這隻在某些計算機終端上。據我所知,我們所有的Excel程序都設置相同。

任何幫助將不勝感激。

回答

0

「運行時錯誤9:下標超出範圍」由下式給出一個數組索引無效/名稱引起..說代碼Sheets("Primary")但沒有紙張命名 ..

具體到的情況下,與給定的信息,我懷疑已更改錯誤發生,因爲工作簿的名字......所以代碼Workbooks("Importer")嘗試尋找工作簿命名進口商和失敗..

+0

蘇西洛感謝,我只是不明白,爲什麼它可以工作在計算機1,2和3上,但是當計算機號碼4使用程序時,它會失敗。每次都是相同的Excel文檔,並且所有表單名稱都沒有更改。工作簿名稱也沒有更改。我已將它放在我們網絡的一個文件夾中,並通過其他人的登錄從不同終端訪問了相同的文件,並且它只是間歇性地失敗。我今天開始工作時會仔細研究一下。 – Rapter

+0

今天我看一下它。我需要做的只是在代碼中添加(.xlsm)到工作簿引用的末尾,這一切似乎都奏效。不知道爲什麼有時需要這樣做,其他人不需要。但現在很好。謝謝Susilo。 – Rapter