gpt4 book ai didi

vba - 非 ActiveSheet 范围内的 QueryTables

转载 作者:行者123 更新时间:2023-12-04 21:40:53 25 4
gpt4 key购买 nike

我一直在放QueryTables结果 ActiveSheet ,但现在我想把它们放在另一张纸上。在下面的代码中,注释掉的行是我最初的,下面的“with”行是我的新尝试。

Dim QuerySheet As WorkSheet

Dim ttt As Integer
Set QuerySheet = ActiveWorkbook.Worksheets("ZZ$Temp$")
QuerySheet.Cells.Clear

QuerySheet.Cells(1, 1) = 1234
QuerySheet.Cells(2000, 50) = 5678

ttt = QuerySheet.Cells(1, 1)

' With ActiveSheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=DataSheet.Range(Cells(row_stock + iMax, 27), Cells(row_stock + iMax + 199, 27)))
With QuerySheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=QuerySheet.Range(Cells(row_stock + iMax, 27), Cells(row_stock + iMax + 199, 27)))
.BackgroundQuery = True
.TablesOnlyFromHTML = False
.Refresh BackgroundQuery:=False
.SaveData = True
End With

我知道工作表存在,我在 1, 1 定义内容和 2000, 50 .

我收到错误 Run-time error '1004' Method 'Range of object '_worksheet' failed.
我也试过激活 QuerySheet和原来的 With .

我如何获得 QueryTables把东西放进去 QuerySheet ?

最佳答案

您需要在某处设置 QuerySheet。只需Dim是不足够的。添加

 Dim QuerySheet As WorkSheet
set QuerySheet = Sheets("querysheet")

(或任何工作表的名称)在您的代码之前。

然后,您必须更改以下内容以仅引用 QuerySheet(适用于 QuerySheet 的所有“单元格”和“范围”使用)。
With QuerySheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=QuerySheet.Range(QuerySheet.Cells(row_stock + iMax, 27), QuerySheet.Cells(row_stock + iMax + 199, 27)))

关于vba - 非 ActiveSheet 范围内的 QueryTables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11970206/

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