简体   繁体   English

VBA Excel QueryTables.add .Refresh BackgroundQuery错误

[英]VBA Excel QueryTables.add .Refresh BackgroundQuery Error

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

The code above creates an error at the specified line. 上面的代码在指定的行上创建了一个错误。 A google search on .Refresh BackgroundQuery shows that it is picky in its functionality in loops. 谷歌搜索.Refresh BackgroundQuery表明它在循环中的功能是挑剔的。 Simply deleting the line makes nothing show up in excel. 简单地删除该行就不会在excel中显示任何内容。

With the current error message the code works fine for the first i value and then breaks. 使用当前错误消息,代码适用于第一个i值,然后中断。

For Answer and comments- TLDR: .Refresh BackgroundQuery:=False will fail if your query input is invalid or malformed. 对于答案和评论 - TLDR:.Refresh BackgroundQuery:=如果您的查询输入无效或格式错误,False将失败。 The problem in this case was the for...next loop was calling cells to use as url's that hand no values in them. 在这种情况下的问题是for ... next循环调用单元格作为url,它们中没有值。 However it will fail anytime the query is malformed. 但是,只要查询格式错误,它就会失败。

All the previous lines inside the With statement are setting properties. With语句中的所有前面的行都是设置属性。
the .Refresh BackgroundQuery := False is a method call. .Refresh BackgroundQuery := False是一个方法调用。

The refresh is supposed to refresh the results. 刷新应该刷新结果。
The background Query is for when quering SQL data and is optional so I think you can leave it off and just have .Refresh 后台查询用于查询SQL数据时是可选的,因此我认为您可以将其关闭并且只有.Refresh

Query Table Refresh Method Help Link 查询表刷新方法帮助链接

Edit It would appear that there is something wrong with the URL and when it goes to refresh it is unable to do it. 编辑看起来网址有问题,当它进行刷新时无法执行此操作。 could be a proxy issue, or not connected to the network, or the URL does not exist. 可能是代理问题,或未连接到网络,或URL不存在。

The only way to resolve this issue is to delete the active query table after each iteration. 解决此问题的唯一方法是在每次迭代后删除活动查询表。 A useful example solution provides: 有用的示例解决方案提供:

https://social.technet.microsoft.com/Forums/office/en-US/956dc1b6-bd37-4b97-a042-ba2a37f729b6/removing-querytables-and-leaving-the-results?forum=excel https://social.technet.microsoft.com/Forums/office/en-US/956dc1b6-bd37-4b97-a042-ba2a37f729b6/removing-querytables-and-leaving-the-results?forum=excel

I'm not sure why my fix worked, but here it is: 我不确定为什么我的修复工作正常,但这里是:

I also used querytables.add within a for loop, and I was adding .asc files. 我还在for循环中使用了querytables.add,我正在添加.asc文件。 This error was only popping up after the last addition--so my program essentially did what I wanted it to, but it would interrupt function. 这个错误只是在最后一次添加后弹出 - 所以我的程序基本上做了我想要的,但它会中断函数。 On the last run through the For loop, I removed the .Refresh BackgroundQuery:=False statement. 在最后一次运行For循环时,我删除了.Refresh BackgroundQuery:= False语句。 It was necessary for it to paste my data for all the previous runs through the For loop. 它必须通过For循环粘贴我之前所有运行的数据。

Basically I replaced this: 基本上我替换了这个:

          .Refresh BackgroundQuery:=False

With this: 有了这个:

          If Index = ctr Then

          Else
               .Refresh BackgroundQuery:=False
          End If

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM