Quantcast
Channel: Excel IT Pro Discussions forum
Viewing all articles
Browse latest Browse all 11829

Excel VBA Code to Update a database query and Pivot Tables does not work.

$
0
0

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.



Viewing all articles
Browse latest Browse all 11829

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>