2013-12-13 1201 views
-1

我遇到了一些VBA代碼的問題。我在Windows 7 Enterprise上運行Excel 2010。VBA:.Refresh運行時錯誤

我試圖從一個文件夾中讀取幾個製表符分隔的文本文件,並將它們放到一個Excel工作簿的單獨工作表中。爲此,我使用查詢表。在調試中,我遇到.Refresh BackgroundQuery:=False問題。當它到達這一行時,它會引發1004運行時錯誤,指出Excel無法找到文本文件來刷新此外部數據範圍。我不知道爲什麼會發生這種情況。我知道查詢表在讀取這行之前不會創建,這會使調試變得困難。這是代碼。任何幫助將非常感激。提前致謝!

Sub LoadPipeDelimitedFiles() 
Dim idx As Integer 
Dim fname As String 

idx = 0 
fname = Dir("C:\files\*.txt") 
While (Len(fname) > 0) 
    idx = idx + 1 
    Sheets("Sheet" & idx).Select 
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fname, Destination:=Range("A1")) 
     .Name = "a" & idx 
     .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 = False 
     .TextFileTabDelimiter = True 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = False 
     .TextFileSpaceDelimiter = False 
     .TextFileOtherDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 
     fname = Dir 
    End With 
Wend 
End Sub 

這裏是修正:

Sub LoadPipeDelimitedFiles() 
Dim idx As Integer 
Dim fpath As String 
Dim fname As String 
Dim f_dummy As String 

idx = 0 
fpath = "C:\files\" 
f_dummy = fpath & "*.txt" 
fname = Dir(f_dummy) 
While (Len(fname) > 0) 
    idx = idx + 1 
    Sheets("Sheet" & idx).Select 
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" _ 
     & fpath & fname, Destination:=Range("A1")) 
     .Name = "a" & idx 
     .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 = False 
     .TextFileTabDelimiter = True 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = False 
     .TextFileSpaceDelimiter = False 
     .TextFileOtherDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 
     fname = Dir 
    End With 
Wend 
End Sub 
+0

根據您的修正,它的高潮,以我在我的答案:) –

回答

2

變化With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fname, Destination:=Range("A1"))

With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & "C:\files\" & fname, Destination:=Range("A1"))

fname只是有文件名的方式而不是全部路徑

還避免使用.Select並完全符合您的要求ObjectsINTERESTING READ

您的代碼可以寫成

​​
+0

非常感謝你說!那就是訣竅。實際上,造成這個問題的原因首先是我在最後的路線上留下了最後一個斜線字符。我將用我的完整代碼更新這篇文章,以展示它現在的樣子。 – user3100444

+1

不,不更新我的帖子:)你可以解釋通過更新你的問題 –