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"
Related
I am not sure if this is too ambitious. Currently I'm generating a bunch of pivot tables through our server (I won't go into detail) but our main stocktaking spreadsheet references these generated pivot tables weekly and pulls their figures through.
i.e using code ='C:\Users\ITS1\Documents\Test\Establishment\190303[Stocktake.xlsx]Data4'!$D$3 which is simple enough - just referencing whatever figure is in that cell on the pivot table.
There is a particular bunch of figures on some pivot tables each week called 'Redemption' which, depending on what other kind of sales an establishment generates, shift to different cells each week. So I can't reference them like other figures which are always fixed to the same cells on every generated pivot table.
Is there a way I can reference the pivot table by the keyword 'Redemption' and take the figure from the cell directly next to it which is the actual figure we need to record?
While this isn't hugely urgent, it would be quite handy. These figures can just be manually entered but just trying out some automation to eliminate my human error :)
If you don't want to have to open the external workbook, this approach might be feasible:
In the external workbook, create a helper sheet that uses a formula to look up the Redemption field value. This could possibly be done with a GetPivotData formula. The exact formula depends on your data and without seeing that I can't write it.
The idea here is that the helper sheet and the location of the cell on the helper sheet does not change. When the pivot table is refreshed, the formula calculates the result.
In the other workbook you can now use an absolute reference to that helper sheet and cell and don't need to open the external file to retrieve the value.
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
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
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
If I manually enter a formula into a column in an Excel table (i.e. ListObject), AutoCorrect applies this formula to the whole column.
Is there any way to control the this behavior via VBA, i.e. can I somehow modify/delete/add this formula?
I know I can simply change the formula of the ListObject.ListColumns(1).DataBodyRange object - but this will overwrite any manually values entered before - while changing the formula in the UI will leave this untouched...
Thanks to Doug's and bonCodigos comments/answers, I found the simple answer:
ListObject.ListColumns("Column name").DataBodyRange.FormulaR1C1 = "new formula"
This will overwrite any manual value (just as the normal behavior with AutoCorrect).
Best would be if you could show us a screen shot of your sheet. Based on that we would have articulated the answer.
Here is with the general assumption. That you have one list object to dump some data to a column and rest of the cells in that column are manually interacted.
You could try the following manually first and see if it works for you. Still the manual one is being overtaken by the code level, then you do this in code level. :)
The main action here is to Stop automatic corrections
Go To -> Tools menu -> Click AutoCorrect Options ->
In the AutoCorrect Tab ->
1- To prevent ALL automatic corrections
Clear the check box for Replace Text as you type
2- To prevent SPECIFIC corrections
Clear the corresponding check box for the option. I believe you are more interested in the latter - specific data range that you just dump via a listobject.
Now here is the code for disabling this feature at code level.
When working with Excel Tables (ListObjects) there are two AutoCorrect options to consider: You can read about those two in details.
* Apply new rows and columns in table
(VBA AutoCorrect.AutoExpandListRange Property)
* Fill formulas in tables to create calculated columns
(VBA AutoCorrect.AutoFillFormulasInLists Property)
Code you may want to use at the top of our listobject is,
Application.AutoCorrect.AutoFillFormulasInLists = False
And totally agree that it would be so much more useful if Application.AutoCorrect. AutoFillFormulasInLists controlled on a table by table basis at the ListObject level. So here is a workaround.
So one way is to clear your table data each time. And when you clear data you can make sure for TABLE TO FORGET forumulae and formatting. So it will clear the contents of the data body range before deleting table contents.
Sub forgetMe()
With Sheet1.ListObjects("myTable")
If Not .DataBodyRange Is Nothing Then
.DataBodyRange.ClearContents
.DataBodyRange.Delete
End If
End With
End Sub
When you input the data, start with auto stuff off.