单元格中来自 url 的动态网络查询

Dynamics webquery from url in cells

我在 Internet 上进行了搜索,但在任何地方都找不到来自单元格链接的动态查询。 在 excel 中,我有生成此数据的网络查询:

我有数据在 excel Sheet("CustomReport"):

SalesOrder  Value1      Value2      Value3     Links
1           Jonas       Station1    8          https://x.com=1
2           Greg        Station1    5          https://x.com=2
3           Anton       Station1    1          https://x.com=3
...         ...         ...         ...        ...

此查询中的行数在刷新时始终不同。

基于这个网络查询,我需要在宏中生成动态网络查询。 例如: DynamicQuery1 将报告 https://x.com=1 中的数据保存到 Sheet 名称 "Orders" 从 A1 开始并结束 A{X} 值(报告具有不同的行数)。

DynamicQuery2 将报告 https://x.com=2 中的数据保存到相同的 Sheet "Orders" 但从 A{X+1} 开始。

我有这样一个宏,但它只适用于第一行。

子测试()

Dim URL As String

URL = Sheets("CustomReport").Range("E2").Value

Sheets("Orders").Select
With ActiveSheet.QueryTables.Add(Connection:="URL;" & URL, Destination:=Range("$A"))
    .Name = "team2289_2"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = False
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlOverwriteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlEntirePage
    .WebFormatting = xlWebFormattingAll
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = True
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

而且我需要每 1 小时刷新一次这个宏。 有没有人可以根据这种方式给我宏?

要遍历单元格,请使用:

dim c as range, DataSpot as range
set c = Sheets("CustomReport").Range("E2")
while c.value <>""
    url = c.value
    set DataSpot=cells(sheets("orders").Range("A1").SpecialCells(xlLastCell).row+1,1)
    ' Web Query Code goes here
    set c=c.offset(1,0)
wend

在您的数据查询中,使用:

Destination:=Range(DataSpot.Address))

子测试()

Dim URL As String

URL = Sheets("CustomReport").Range("E2").Value

Sheets("Sheet1").Select
Dim c As Range, DataSpot As Range
Set c = Sheets("CustomReport").Range("E2")
While c.Value <> ""
    Set DataSpot = Cells(Sheets("Sorders").Range("A1").SpecialCells(xlLastCell).Row + 1, 1)
        With ActiveSheet.QueryTables.Add(Connection:="URL;" & URL, Destination:=Range(DataSpot.Address))
         .Name = "team2289_2"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = False
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlOverwriteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlEntirePage
            .WebFormatting = xlWebFormattingAll
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = True
            .WebDisableRedirections = False
             .Refresh BackgroundQuery:=False
        End With
        Set r = r.Offset(1, 0)
Wend

结束子

我把你的代码和我有调试: 设置 r = r.Offset(1, 0)

我做错了什么?