Excel: Automatically update unshared PivotTables without ungrouping fields - excel

I have some very simple code to automatically refresh my pivot tables when the data source is updated:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Sheet2.PivotTables("PivotTable1").PivotCache.Refresh
Sheet3.PivotTables("PivotTable2").PivotCache.Refresh
Sheet4.PivotTables("PivotTable3").PivotCache.Refresh
End Sub
Whenever it refreshes, my fields ungroup. For example one is in months the other is in weeks, etc. but when it refreshes they all ungroup and show each individual date. Any ideas?

One thing you can do is to record a macro that groups the data into the format that you want. Then copy the macro section that groups the selection after each pivot table refresh command. So essentially you refresh and regroup each time.
A link that shows how you can use VBA to group pivot table data is http://www.ozgrid.com/forum/showthread.php?t=55370
I hope this helps

Related

Is there a way to add one new row to a selected range each time code is run? And also save this upon exit?

I am wondering how I can increase a selection range by 1 row each time the code is run but for it to always increase selection by 1.
What happens is I get new data which is pasted into a table and the pivot table is refreshed. Upon refresh, there are new entries in the pivot table. I would like to select a certain range of cells when new data is added and copy this selection to another page (but not all the new data as some of the data is unreliable)
Code so far is:
Sub A_Data_Refresh_Select ()
ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh
Range("B19:G509").Select
I know this just simply selects the range, but it was just done using a macro so I could edit it. Ideally I would like to select all the data apart from the last 2 or 3 rows each time. Maybe that is an easier way to think of it?
Thanks for the help.

Refreshing Pivot Tables

I am trying to write a VBA script (MS Excel) which allows me to automatically refresh Pivot Tables when the data source is refreshed. I have used the code below where the Pivot Tables are in the "Sheet1 "worksheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Worksheets("Sheet1").PivotTables("PivotTable1").RefreshTable
End Sub
This code only works when I manually change data within the data source. However, I want the Pivot Tables to refresh when data in the data source changes due to VLOOKUP functions and not a manual input.
Does anyone perhaps have a solution for this?
Thanks.
Have you tried this command? I have a sharepoint site that downloads to my excel file. I added this line of the code to refresh the data:
Workbook.Connections("externaldata").Refresh

Auto-Refresh Pivot Table with VBA, when Source Table contains Formulas

My goal is that a Pivot Table updates automatically when I make changes to the source data. The "normal" code (see below) discussed here in the forum does not work.
Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets("Pivot").PivotTables("Test").PivotCache.Refresh
End Sub
My setup is the following:
In a tab called "Overview" I have numbers (%) I like to adjust to test different scenarios.
In tab "Constituents" I have my table (called "Input") which is the basis for my Pivot Table. The numbers in this table change, whenever I make changes to the percentages in tab "Overview" (obviously numbers in table are generated with formulas).
In tab "Pivot" I have my Pivot Table called "Test".
I have tried my luck with the code stated above. If I overwrite a formula in my table, the Pivot updates automatically. This means that the code works for that scenario.
However: If the numbers in the table change due to the formula (meaning a change in tab "Overview"), my Pivot Table does not update.
Does someone know a way to solve this problem? Thanks a lot in advance!
I think you have to add the Worksheet_Change function in both sheets in "Constituents" as well as in "Overview"

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

Show multiple drillthrough tables in a single Excel sheet

I need to create a report (for printing) that has two sections. The top section contains the scorecard, and the bottom section contains the drillthrough information for any "red" items in the scorecard.
The scorecard is currently a PowerPivot table with conditional formatting. If the scorecard has 3 "red" cells, then I can double-click on each of the red cells to drillthrough to the details. However, I would like to display those 3 drillthrough tables in the same worksheet below the scorecard, so I can print both the scorecard and the drillthrough details all at once.
I'm looking for a solution that is automated. Each time I print out the report, I can do a lot of copy and paste, but I rather not. I'm looking for a solution that is as automated as possible - so the bottom section of the report will always contain details for anything that is red in the scorecard. I'm open to using VBA.
Thanks!
I would suggest to use VBA. However first store a list of sheets in one of your sheets for your VBA code to use. Create the following subroutine in your Workbook code section to have the trigger for when a new sheet is selected:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Now add code to this subroutine to verify if the sheet selected was already present in your workbook by checking against the list.
If it wasn't in the list yet, check if there is a table on the sheet.
If there is a table on the sheet have its content copied/moved to your report sheet to a specific range and delete the drill through sheet again (and remove it from the list). Have the code select your report sheet again!
If there was NO table on the sheet add it to the list of sheets

Resources