refresh both the External data source and pivot tables together within a time schedule

前端 未结 5 2063
感动是毒
感动是毒 2020-12-05 09:09

In my last post Auto refresh pivottables data in excel on first run, i found that on my first execution the query from the Externa

5条回答
  •  广开言路
    2020-12-05 09:22

    Under the connection properties, uncheck "Enable background refresh". This will make the connection refresh when told to, not in the background as other processes happen.

    With background refresh disabled, your VBA procedure will wait for your external data to refresh before moving to the next line of code.

    Then you just modify the following code:

    ActiveWorkbook.Connections("CONNECTION_NAME").Refresh
    Sheets("SHEET_NAME").PivotTables("PIVOT_TABLE_NAME").PivotCache.Refresh
    

    You can also turn off background refresh in VBA:

    ActiveWorkbook.Connections("CONNECTION_NAME").ODBCConnection.BackgroundQuery = False
    

提交回复
热议问题