How can I post-process the data from an Excel web query when the query is complete?

后端 未结 2 1743
抹茶落季
抹茶落季 2020-12-03 20:44

As a spreadsheet developer, I am trying to stitch together two sets of rows: one from a web query to a web service I own, and the other a set of manual rows added by the spr

相关标签:
2条回答
  • 2020-12-03 21:07

    Excel supports the ability to open a URL as another Excel workbook, via the Workbooks.Open method:

    From MSDN:

    Sub OpenUSDRatesPage()
       Dim objBK As Workbook
       Dim objRng As Range
    
       'Open the page as a workbook.
       Set objBK = Workbooks.Open("http://www.x-rates.com/tables/USD.HTML")
    
       'Find the Canadian Dollar cell.
       Set objRng = objBK.Worksheets(1).Cells.Find("Canadian Dollar")
    
       'Retrieve the exchange rate.
       MsgBox "The CAD/USD exchange rate is " & objRng.Offset(-6, -1).Value
    End Sub
    

    The call is synchronous, so you can operate on the resulting data in the new workbook immediately after the Open call.

    While the workbook is loading, Excel will display a progress bar. When you're done, you can call .Close to close the web data workbook. (e.g., for the MSDN example, you'd call objBK.Close when you're done.)

    The caveats of using this approach:

    1. You're on the hook to migrate the data from the web workbook to your own (ThisWorkbook) yourself, unlike a refreshable Excel Web Query that has a set destination.
    2. If your web endpoint has a document name that matches the name of a document open in Excel, the user will get a warning that a document with the same name is open.
    0 讨论(0)
  • 2020-12-03 21:09

    An Excel web query utilizes an object called a QueryTable to carry out the business of retrieving and displaying the data.

    A QueryTable can be accessed by VBA.

    And just like the chart object a querytable object has events that can only be responded to by using the WithEvents keyword from a class module, like so:

    Private WithEvents MyQueryTable As QueryTable
    
    Private Sub MyQueryTable_AfterRefresh(ByVal Success As Boolean)
        'Do your post processing here...
    End Sub
    
    0 讨论(0)
提交回复
热议问题