来自单元格中url的动态webquery

时间:2018-01-17 14:04:12

标签: excel vba excel-vba dynamicquery excel-web-query

我搜索了互联网,但我找不到来自单元格链接的动态查询。 在excel中我有webquery生成这些数据:

我在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
...         ...         ...         ...        ...

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

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

DynamicQuery2将报告https://x.com=2中的数据保存到同一个工作表"订单"但是从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$1"))
    .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小时刷新一次这个宏。 任何人都可以基于这种方式给我宏?

2 个答案:

答案 0 :(得分:0)

要遍历单元格,请使用:

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))

答案 1 :(得分:0)

子测试()

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

End Sub

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

我做错了什么?