How to Dynamically update multiple PivotCaches in one workbook using VBA? - excel

Every month, I run a massive Scorecard report. The raw data contains 13 workbooks that are exported as .csv files.
I use Power Query to manipulate and Append this data into 4 different worksheets.
To keep things clean, I then use a Macro to move the 4 Data Arrays into a Scorecard Workbook (which is shared with the team)
The Scorecard Workbook has 4 tabs with the Data Sheets: wsTotalUS, wsCombined, wsDFCoreMkts, wsNovCoreMkts.
The 4 Data Sheets to create 4 different Pivot Caches.
wsTotalUS has a Pivot Table on Sheet5
wsCombined has a Pivot Table on Sheets 6-13
wsDFCoreMkts has a Pivot Table on Sheet 14
wsNovCoreMkts has a Pivot Table on Sheet 15
Each month the data Rows can change (The Columns will always be the same - “A:AP”)
I have researched how to dynamically update pivot tables. I know some articles suggest changing the source data into a Table. I would like to avoid this due to the amount of time it would take to move the Data over to the Scorecard.
For example: wsTotalUS has 47,707 rows & wsCombined has 123,153 rows
One article I found had the code written on the Source Data Sheet as a Private Sub Worksheet_Deactivate().
However, this would only use one Data Sheet, and update all of the Pivot Tables in the workbook when you switch from the data sheet.
How to Dynamically Update All Pivot Tables Data Source in Excel
I found these articles, but they one only refers to one Data Sheet and one Pivot Table.
How to Automatically Update a Pivot Table Range in Excel
Dynamically Change a Pivot Table's Data Source Range with This VBA Code
This article uses the Worksheet_SelectionChange event. But again, only one Data Sheet is implied here.
Refresh Pivot Tables Automatically when Source Data Changes
How do I go about dynamically updating 4 different PivotCaches within a workbook?
Should I just run 4 separate sub routines?
UPDATED:
Based on Tim's suggestion:
Under Formulas -> Named Range:
TotalUS =OFFSET('Total US'!$A$1,0,0, COUNTA('Total US'!$A:$A),COUNTA('Total US'!$1:$1))
Then go to the Pivot Table that references this data, Change Data Source = TotalUS

Related

What formula would I use to make a column automatically populate in excel when new data is loaded into a worksheet?

I am creating a report in Excel with 3 separate worksheets, the main one where data is reported and calculated, one for new data and one for old data. I want values in one column in the main worksheet to update when new information is moved into the new data worksheet but still see values that were in the old data that may have dropped off the new list.
Connect to an Excel table or range
Click the Data tab, then > From Table/Range.
https://support.office.com/en-ie/article/connect-to-an-excel-table-or-range-power-query-67538004-cd12-4b61-9cee-8720db1c7136

Report connections for Pivot table slicer doesn't show all pivot tables?

I have 3 sheets with several pivot tables on each and one sheet with the source data. These 4 sheets were copied directly from another workbook. When I copied the worksheets over the source data for each pivot table was still the original source data sheet in the original workbook. Therefore, I changed each pivot table's source to the copied over raw data sheet. Whilst doing this I had to disconnect all the pivot table slicers in order to be able to change the source.
Now, this is where my problem lies; When I go back to reconnect the slicer only one of the pivot tables shows up in the list, when in fact I need to connect the slicer to several. What do I need to do?
Note: I did all of this using a macro that I created so I am open to using macros.
I was able to resolve this issue with the following steps;
Delete all slicers
Change the Table name:
For each pivot table, click on change data source button. The new name should already be in there so just press enter. If you didnt delete all slicers it will throw an error, indicating that it is only identifying other pivot tables with the same datasource now.
Create a new slicer and reconnect to all tables.
Note that step 2 may not be required but that is the workflow that I followed.
I had the same issue with not all pivots showing up and noticed that while the SOURCE was the same in all, the RANGE that I had selected in the source was different. I reset all the ranges in the pivot tables to be the same and all tables now showed up in the slicer for selection.
I had the same issue, and fixed it using the following idea.
Reassign the Pivot Tables' data sources (first define one Pivot Table source, and then copy the PivotCaches to the others):
Sheets("nameofthesheet").Select
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache _
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"name_of_the_table")
ActiveSheet.PivotTables("PivotTable2").CacheIndex = ActiveSheet.PivotTables("PivotTable1").CacheIndex
ActiveSheet.PivotTables("PivotTable3").CacheIndex = ActiveSheet.PivotTables("PivotTable1").CacheIndex
ActiveSheet.PivotTables("PivotTable4").CacheIndex = ActiveSheet.PivotTables("PivotTable1").CacheIndex
Rebuild the Slicers (recording a Macro will be useful!)
Make sure all your pivot table have the same data range, if not change the data range, when all pivot table have the same data range, then it will appear at the connection for the slicer

Changing the data source of pivot table in VBA

My problem goes like this:
I have 2 worksheets in 1 workbook which are the "Source Data" and the "Pivot Sheet". Everytime I generate(I generate data every week and it's in macros), data are added to Source Data sheet but the Pivot Table in the Pivot Sheet remains the same. I think it's quite handy if I will still have to Change Data Source everytime I'll generate the report. I want it to automatically adjust it's data source until the last used cell. How to do it? Columns used as Source are from Column C to W Please help! :(
If you create tables (ctrl + t) for your source data and direct your pivot to read those tables, they should automatically expand as new data is entered. Then it should be as simple as Thisworkbook.Worksheets("Pivot").PivotTables("PivotTable1").PivotCache.Refresh (based on the macro recorder)

Pivot and automatic inclusion of new row nr data in Excel

I needed some guidance from your more experience excel pros. I have two sheets in my excel file.
Data
Pivot Data
I have my data in the "Data" sheet and I have created a Pivot sheet for sheet Data.
Everyweek, I delete my data in the Data sheet and paste new data.With this, my rows become more. to update my pivots, I need to go to data source and update it so it include all the rows.
Is there something I can do or write so the pivots automatically adjust to the size of the new paste data in the Data sheet?
If before deleting the data, it was on row 400, I then paste new data and the row number is now 800, how can I automate this process rather than going to each pivot and updating manually to the new row number so it captures all the new data?
Change your Data Source range to exclude row numbers, so say:
Sheet3!$A:$D
instead of:
Sheet3!$A1:$D400
This will however introduce a row in your PT of (blank) (if not already present there), though you might filter to hide that row.

Can I copy query tables (and connections?) to other workbooks?

I have a workbook (wkbk1) that has a sheet with eight different query tables (a.k.a. data tables) on it -- NOT PIVOT TABLES. They all point to the same external Excel workbook (wkbk2) for their data which is a single sheet with a group of named ranges.
The data for each table in wkbk1 comes from a named range in wkbk2. How can I copy the sheet of eight different tables (from wkbk1) to other workbooks (wkbk3, wkbk4, etc)?
Is it even possible for the query table and its data connection to transfer with the sheet? So far I can't do it manually (no coding). If coding is required I'm fine with that, just need help with it. I have intermediate VBA coding experience.
I'm using Excel 2007/2010.
In Excel 2010, I right click on the sheet tab and choose Move or Copy. Select the destination workbook and check "Create a Copy". The new sheet has the QueryTable on it and refreshes as expected.
So I think copying the sheet will copy the QueryTable and all its properties.
My QueryTable used MSQuery and pointed to another Excel workbook.

Resources