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