2011-04-05 641 views
4
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,它们没有赋值。但是,只要查询格式不正确,它就会失败。

+0

是什么.REFRESH BackgroundQuery连做? – iwishiwasacodemonkey 2011-04-05 23:54:48

回答

4

With语句中的所有前面的行都是设置属性。
.Refresh BackgroundQuery := False是一个方法调用。

刷新应该刷新结果。
背景查询quering SQL数据时,是可选的,所以我觉得你可以不管它,只是有.REFRESH

Query Table Refresh Method Help Link

编辑 这样看来,有什么不对的URL以及何时刷新它无法做到这一点。可能是代理问题,或者没有连接到网络,或者URL不存在。

+0

请记住代码使用电子表格中提供的网址,因此如果您希望代码正常工作,则需要输入您自己的内容我正在使用“http://flvtubesearch.co/?tmp=toolbar_FlvTube_homepage&prt=flvtubetb04ie&clid=acd050b75c114e5ba9a270e770c9cf6f” url和“?tmp = toolbar_FlvTube_homepage&prt = flvtubetb04ie&clid = acd050b75c114e5ba9a270e770c9cf6f” 作为路径 – iwishiwasacodemonkey 2011-04-06 01:48:34

+0

也将.refresh backgroundquery:= False更改为.refresh = false失败,并给出错误“对象不支持此方法” – iwishiwasacodemonkey 2011-04-06 01:50:27

+0

在另一个笔记上if你改变下一个循环为i = 2到2的代码运行只是找到,所以问题是以某种方式相关的...下一个循环 – iwishiwasacodemonkey 2011-04-06 01:51:47

0

我不知道为什么我的修复工作,但在这里它是:

我也用querytables.add内的for循环,而我加入。 asc文件。这个错误只是在最后一次添加后弹出 - 所以我的程序基本上做了我想要的,但它会中断函数。在For循环的最后一次运行中,我删除了.Refresh BackgroundQuery:= False语句。它有必要通过For循环来粘贴我所有以前运行的数据。

基本上我换成这样的:

  .Refresh BackgroundQuery:=False 

有了这个:

  If Index = ctr Then 

      Else 
       .Refresh BackgroundQuery:=False 
      End If