动态查询VBA如何将值从一个查询添加到第二个

时间:2018-01-17 19:20:34

标签: excel vba excel-vba

我在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}开始。

这是宏:

Sub Test()
    Application.DisplayAlerts = False
    ThisWorkbook.Sheets("Orders").Delete
    Application.DisplayAlerts = True
    ThisWorkbook.Sheets.Add.Name = "Orders"
    Dim c As Range, DataSpot As Range
    Set c = Sheets("CustomReport").Range("E2")
    While c.Value <> ""

    Dim URL As String
    URL = c.Value
        Set DataSpot = Cells(Sheets("Orders").Range("A1").SpecialCells(xlLastCell).Row + 1, 1)


                    Sheets("Orders").Select
                    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 c = c.Offset(1, 0)
    Wend


End Sub 

现在我需要为这个宏添加新公式。 这是&#34; Links&#34;

的结构报告

来自工作表&#34;订单&#34;

的示例工作表
WorkOrder  ID          Item2       Column4    Level3    <----- Report Sales Order 1 
Number4    TOPLEVEL    Jonas       Station1   5
212121212  2           Station1    5          2
31212121   3           Station1    1          4
...         ...         ...         ...        ...

TextSome   TOPLEVEL    Jonas       Station1   5         <----- Report Sales Order 2
212121212  2           Station1    5          2
31212121   3           Station1    1          4
...         ...         ...         ...        ...

...        ...          ...         ...        ...<----- Report Sales Order 3

DADADA   TOPLEVEL    Jonas       Station1   5         <----- Report Sales Order 4
212121212  2           Station1    5          2
31212121   3           Station1    1          4
...         ...         ...         ...        ...

我需要添加公式,当每个循环生成时,makro将从工作订单添加到新列值,其中ID =&#34; TOPLEVEL&#34;,但如果报告不存在则将空白值添加。

   SalesOrder    Value1    Value2      Value3     Links             WOTOP
   1             Jonas     Station1    8          https://x.com=1   Number4
   2             Greg      Station1    5          https://x.com=2   TextSome
   3             Anton     Station1    1          https://x.com=3   blank
   4             Anton     Station1    1          https://x.com=3   DADA
   ...           ...         ...         ...        ...

`

0 个答案:

没有答案