gpt4 book ai didi

VBA Excel QueryTables.add .刷新BackgroundQuery错误

转载 作者:行者123 更新时间:2023-12-02 10:36:56 29 4
gpt4 key购买 nike

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 中就不会显示任何内容。

根据当前错误消息,代码对于第一个 i 值工作正常,然后中断。

对于答案和评论-TLDR:如果您的查询输入无效或格式错误,.Refresh BackgroundQuery:=False 将失败。本例中的问题是 for...next 循环正在调用单元格用作 url,但其中不包含任何值。但是,只要查询格式错误,它就会失败。

最佳答案

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

刷新应该刷新结果。
后台查询用于查询 SQL 数据时,并且是可选的,因此我认为您可以将其保留,只使用 .Refresh

Query Table Refresh Method Help Link

编辑看起来 URL 有问题,刷新时无法执行。可能是代理问题,或者未连接到网络,或者 URL 不存在。

关于VBA Excel QueryTables.add .刷新BackgroundQuery错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5559644/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com