I have an Excel file (using Excel 2010) with a connection to a SQL database. The table created by that connection is driving 2 Pivot tables. I have created VBA script to refresh both of these on the push of a button. Unfortunately it takes multiple pushes of the button to properly update everything. When I am in debug mode and stepping through the script (using F8 "Step Into) the script runs as it should. I have tried adding time delays between the SQL refresh and the Pivot refresh. That does not work. My Code is:
Sub Update()
' This refreshes the data from the MiSYS Database
Sheets("RAW").Select
ActiveWorkbook.Connections("xxxx.xxxx.com dB_NAME").Refresh
' This refreshes the two Pivot Tables
Sheets("PIVOT-Stock").Select
Range("D5").Select
ActiveSheet.PivotTables("Pivot_Stock").PivotCache.Refresh
Sheets("PIVOT-WIP").Select
Range("D6").Select
ActiveSheet.PivotTables("Pivot_WIP").PivotCache.Refresh
' This updates the date and time
Sheets("CALCULATION").Select
Cells(6, 13) = Now()
End Sub
I have also tried using ActiveSheet.PivotTables("Pivot_Stock").Update instead of teh PivotCache.Refresh. This does not work either.