Sub Macro1()
Dim URL As String
Dim Path As String
Dim i As Integer
For i = 2 To 50
If Range("Prices!E" & i).Value <> 1 Then
URL = Range("Prices!D" & i).Text
Path = Range("Prices!F" & i).Text
End If
Sheet19.Activate
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & URL _
, Destination:=ActiveSheet.Range("$A$1"))
.Name = _
"" & Path
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
//'In the Line above the above
//'Run time error '1004
//'An unexpected error has occured
End With
Next i
End Sub
上面的代碼在指定的行創建一個錯誤。在.Refresh上的谷歌搜索BackgroundQuery顯示它在循環中的功能是挑剔的。簡單地刪除這條線就不會在excel中顯示出來。VBA Excel QueryTables.add .Refresh BackgroundQuery錯誤
使用當前的錯誤消息,代碼對第一個i值正常工作,然後中斷。
對於答案和評論- TLDR:.Refresh BackgroundQuery:= False如果您的查詢輸入無效或格式錯誤將失敗。在這種情況下的問題是for ... next循環調用單元格作爲URL,它們沒有賦值。但是,只要查詢格式不正確,它就會失敗。
是什麼.REFRESH BackgroundQuery連做? – iwishiwasacodemonkey 2011-04-05 23:54:48