Pivot is not refreshing - excel

There are 3 sheets in my excel file.
One (Qry Results) is Query, the next one (Qry) is importing the data from Query by formula, the last one (Results) has a Pivot table based on Qry data.
Qry Results needs to be refreshed in VBA. It is working fine.
Qry data is udpated automatically by formula.
If Qry data is 100, but I entered formula in 150 lines.
When I run VBA by step by step, the PIVOT is refresh properly. But when I run automatically, the pivot is not refreshing.
`Refresh Query
Sheets("Qry Results").Select
Range("A4").Select
ActiveWorkbook.RefreshAll
`Refresh Pivot
Sheets("Result").Select
Range("A12").Select
ActiveWorkbook.RefreshAll
Or
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh

You may have background refresh enabled in the Power Query. With this setting active, the Power Query starts to refresh, but the pivot table refresh is kicked off basically at the same time, before the new data is loaded by the Power Query.
To remedy that, you can disable background refresh on the Power Query. Right-click the Power Query in the side panel and select Properties. Untick the box.
Now the pivot table refresh will run only after the Power Query refresh has finished.
-- And, by the way, there is no need to select anything before running ActiveWorkbook.RefreshAll and there is no need to refresh all twice. RefreshAll refreshes everything. If you want to use a particular order, then you can refresh one by one with code like this:
ActiveWorkbook.Connections("Query - Table1").Refresh
ThisWorkbook.Worksheets("Sheet2").PivotTables("PivotTable1").PivotCache.Refresh

Maybe you could try something like this (in order to wait for the query refresh to happen):
Sub RefreshQuery()
'Code to refresh query here
End Sub
Then, in the sheet with the query results:
Private Sub Worksheet_Change(ByVal Target As Range)
'Code to update pivot here
End Sub

Related

PivotTable Not Refreshing With Data

I have an Excel document with 4 Worksheets.
On Sheet1 I set my start & end dates for my data.
On Sheet2 is the data itself (provided by an external SQL database).
On Sheet3 & Sheet4 are PivotTables that use the second sheets data as their source.
I have created a VBA macro button on Sheet1 to refresh all.
I'll change my date range on Sheet1 then click the button but it only refreshes the data on Sheet2 and not the PivotTables on Sheet3 & Sheet4. I have to click the button a second time to refresh the PivotTables with the new data.
Here is the VBA code:
Sub Button2_Click()
Application.ScreenUpdating = False
ActiveWorkbook.RefreshAll
Application.ScreenUpdating = True
End Sub
Even manually using the Refresh All button has the same outcome.
It seems when you do a refresh all it is trying to refresh everything in parallel, probably to save time. However, I think since you have pivot tables which are dependent on data which needs to refresh from the DB, you have to ensure that your external data is refreshed prior to attempting to refresh the pivot tables.
I was able to reproduce the issue and doing the below resolved it for me. Just have to replace "Query1" with the name of your query.
ActiveSheet.ListObjects("Query1").QueryTable.Refresh BackgroundQuery:=False
For Each pc In ActiveWorkbook.PivotCaches
pc.Refresh
Next
Edit:
To get the query name, you can go to the "Data" tab on your ribbon you should see a button that says "Queries & Connections" clicking on that will expand a pane on the right side of the excel screen. In that pane clicking on the Queries tab on the top will give you a list of your queries that are set up
Screenshot of my QueryTable name

show data refreshed time in excel

I have an excel which has sql queries associated with it. I open the excel , click on Data->Refresh All and send across to the users.
When I click on refresh All , I want the refreshed date to appear in a cell on that sheet. Can you give some hint on this.
Add another SQL Query somewhere else in your workbook
SELECT GETDATE()
Make sure you refer to the same database your original query is referring to.
when you refresh the spread sheet the date will be updated as well.
Select Data ribbon
From Other Sources
From Microsoft Query
Choose DataBase (same as one used for the original Query)
"Query Wizard - Choose Columns" Click Cancel
"Do you want to continue editing the data in Query" Click Yes
"Add Tables" Click Close
Click on "SQL Button" and add the query there "SELECT GETDATE()"
Click OK
Click on Return data to Microsoft Excel
Rather than using the Ribbon to perform the Refresh, try a little macro like:
Sub Macro1()
ActiveWorkbook.RefreshAll
With Range("A1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy hh:mm:ss"
End With
End Sub
Pick whatever cell you like.
after searching various Excel related sites (mrexcel.com, exceltown.com and answers.microsoft.com) i've decided that the best solution (for me) does NOT need Macro or another query:
add as a last column in your existing query whatever function provides the current date and time [for Ingres it's date('now')]
hide the column from the resulting table
in the cell that you desire to show the last date & time that your table was refreshed enter the formula max(j:j). substitute j with the column that you've hidden.
simple :)
This was the easiest way for me to get a refresh date. Add a =Date.Time.LocalNow() column. Hide Column. https://exceloffthegrid.com/power-query-get-refresh-date-time/

Refresh pivot tables but not external data source

I have a spreadsheet with multiple tables, where the data is pulled from an external data source (SQL database). The connections/tables refresh by changing an option in a drop down box and then pressing a button to run a VBA.
Attached to each of these tables is a pivot table. The pivot tables don't refresh with the tables. If I try pressing refresh all I get the error;
'Data Source name not found and no default driver specified'
However if I go through the spreadsheet and hit refresh on each individual pivot table they update without the error.
So either I need some way to get the pivot tables to refresh with the tables or have a button that refreshes only the pivot tables and not the external data connections.
Any ideas appreciated, I don't know where to begin with this one!
You can refresh a given PivotTable on Sheet1 like this:
Sheet1.PivotTables(1).RefreshTable
That will refresh the first PivotTable on Sheet1. Change the index number for a different one.
Or...
You can refresh all of the PivotTables on a give sheet by calling this routine:
Sub RefreshPivotTables(ws As Worksheet)
Dim pt As PivotTable
For Each pt In ws.PivotTables
pt.RefreshTable
Next
End Sub
You would call the above routine from the same code associated with the button mentioned in your question that updates the tables.
Or...
If you'd like to update all of the PivotTables in a workbook, you can use this version of the routine:
Sub RefreshPivotTables(wb As Workbook)
Dim ws As Worksheet
Dim pt As PivotTable
For Each ws In wb.Worksheets
For Each pt In ws.PivotTables
pt.RefreshTable
Next
Next
End Sub
You would call this version like so:
RefreshPivotTables ThisWorkbook
Becky: Any reason you don't populate those PivotTables directly from the SQL query? Unless you need those tables there for some reason I'd suggest just ditch 'em, and simply turn the data directly into PivotTables. Otherwise you're effectively saving the same data in the file twice. (Or three times, if you haven't unchecked "Save source data with file" under PivotTable>Data>Options.
If you do need to refresh them - and if there are multiple PivotTables connected to each Table - then it is more efficient to iterate through the underlying PivotCaches and refresh any where the sourcetype is an Excel Range. In VBA speak that's where pc.SourceType = xlDatabase
Sub Refresh_PivotCaches()
Dim pc As PivotCache
For Each pc In ActiveWorkbook.PivotCaches
If pc.SourceType = xlDatabase Then pc.Refresh
Next pc
End Sub
If you instead iterate through each and every PivotTable, then if multiple PivotTables are connected to one PivotCache you end up doing more refreshes than you need. For instance, if you have 10 PivotTables that all point to the same table, you do not need to refresh those 10 PivotTables individually. Rather, you just need to refresh the one PivotCache that they all share. If you were to refresh those 10 PivotTables individually, then in effect you are refreshing each of those 10 PivotTables 10 times.
Granted, if your PivotTables are small you won't notice any difference between my code and Excel Heros.
I added the following at the end of my code which seemed to work fine.
Dim PT As PivotTable
Dim WSH As Worksheet
For Each WSH In ThisWorkbook.Worksheets
For Each PT In WSH.PivotTables
PT.RefreshTable
Next PT
Next WSH
I know this is an old post, but sharing in case this helps anyone researching this like I was -- This may be new to Excel 2016 (I don't have access to older versions currently to test), however I found that you can disable the "Refresh with Refresh All" setting on your external data sources. This will then allow you to use the "Refresh All" from the Data Tab to update all your pivot tables at once, without updating the external data source query again:
Data Tab > Connections Section > Open Connections
Select a connection in the Workbook Connections window and click Properties...
In the properties window, uncheck the box for "Refresh this connection on Refresh All"
Repeat for any additional External Connections you don't want to update automatically
I use this in a reporting macro by calling the manual query updates once:
(selecting a cell in each table created by a query)
ActiveWorkbook.Sheets("Completed").Select
Range("A2").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
And then calling refresh all as needed to update my pivot tables:
ActiveWorkbook.RefreshAll

Excel powerpivot, show when the data was updated?

I have an Excel sheet with powerpivot tables and charts. I want to show when the data was updated.
I use VBA code and a button to update the data, and I use the function now() in a cell which is updated when the code is running.
I have two problems.
When I open the Excel file the date is updated even if I do not update the data on start. (I do not want the data to update on start)
When the update fails the date is still updated.
My VBA code look like this:
Range("table name").Select
ActiveSheet.PivotTables("PivotTable2").PivotCache.refresh
Range("date cell").Select
How to avoid update when the data is not updated?
Instead of solve it as user3819867 suggested, I solved it by adding the information from the database, which is a SQL server database so I added a view with one colum and one row (with a getdate()) and send it along with the other tables so I get one extra table named "date".
How about its event?
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Range("date cell").Select
End Sub

VBA: refreshing just one pivot table for each external data source

Okay so I have the following code to refresh all the pivot tables in the active workbook:
For Each oSht In Active Workbook.Worksheets
For Each oTab In oSht.PivotTables
oTab.RefreshTable
Next oTab
Next oSht
However, I feel that this is inefficient because, when you refresh a pivot, it automatically refreshes all other pivots connected to the same data table.
I would therefore like to refresh only one pivot table for each external data table i have in the active workbook but cant work out a way of implementing this...
You can use the ThisWorkbook.PivotCaches collection and refresh the cache instead of each table.

Resources