Pivot table Values are now screwy - excel

In Excel 2003, I have a macro that accidentally tried selecting a page field item (New York) that didn't exist. This renamed the currently selected page field (Alabama) to the value I tried to select (New York).
I now have 3 or 4 values that are now wrong. Is there a way to refresh these pivot table values to the correct values without recreating the pivot table from scratch?
Thanks

Use this code -
Sub Clean_AllPivotsOnSheet()
Dim pt As PivotTable
For Each pt In ActiveSheet.PivotTables
pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
Next pt
End Sub
Then refresh the Pivot Table.

Related

Identifying the column of the source data in a pivot table using VBA code

This should be very simple, but it is hard for me. I have a Pivot Table. I just want to know, using VBA, which column of the original data was used to totalize the pivot table. I need the column letter or number of such data field.
Thanks in advance
SourceName is the property you want I think
Dim pt As PivotTable, pf As PivotField
Set pt = ActiveSheet.PivotTables(1)
For Each pf In pt.DataFields
Debug.Print pf.Name, pf.SourceName
Next pf

Looping VBA code for dynamic pivot table update

I have found a solution to dynamically adjust pivot table filters from an excel cell which is working great; See here:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As
Range)
'This line stops the worksheet updating on every change, it only
updates when cell
'D1 or D2 is touched
If Intersect(Target, Worksheets("test").Range("D1:D2")) Is Nothing
Then Exit Sub
'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String
'Here you amend to suit your data
Set pt = Worksheets("test").PivotTables("PivotTable2")
Set Field = pt.PivotFields("Name")
NewCat = Worksheets("test").Range("D1").Value
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
End Sub
I am trying to figure out how to loop this for example a defined number of times (ie 60) to control a large number of pivot tables all referring to different cells.
the plan is to have a list of employees and each pivot table will show data relevant to that employee, but the employee list will be dynamic.
EDIT:
Please see image of what i am trying to achieve;
The end goal is to set up page breaks and quickly refresh and print off individual skill levels for staff appraisals.
Looking at your image, this seems to be an XY Problem
You want a list of Users and their Skills. You have decided that this requires Multiple PivotTables. I propose instead a multi-column PivotTable, with a Blank Line between Users:
In addition to turning off Subtotals for "Name", you also need to "Insert blank line after each item label" and not "Display subtotals at the top of the group":
(Incidentally - when working from Multiple PivotTables, especially ones that use the same PivotCache, the PivotTable.ManualUpdate property is very useful to reduce redundant recalculation!)

How To Filter Column Labels With VBA In An Excel Pivot Table

In Excel I have been able to filter the row labels in a pivot table with this code:
Dim PT as PivotTable
Set PT = ActiveSheet.PivotTables("Pivot1")
With PT
.ManualUpdate=True
.ClearAllFiters
.PivotFields("App").PivotFilters.Add Type:=xlCaptionDoesNotContain, Value1:="(Blank)"
End With
I need to do the same filter for the column labels where my field name is Type.
I tired this:
Dim PT as PivotTable
Set PT = ActiveSheet.PivotTables("Pivot1")
With PT
.ManualUpdate=True
.ClearAllFiters
.PivotFields("App").PivotFilters.Add Type:=xlCaptionDoesNotContain, Value1:="(Blank)"
.PivotFields("Type").PivotFilters.Add Type:=xlCaptionDoesNotContain, Value1:="(Blank)"
End With
However I get an error, "unable to get the PivotFields property of the PivotTable class" when it hits the below line:
.PivotFields("Type").PivotFilters.Add Type:=xlCaptionDoesNotContain, Value1:="(Blank)"
What syntax do I use to filter the column label?
There's nothing wrong with that syntax, near as I can tell. Are you sure you have the name right? Best way to check is to fire up the macro recorder, perform the action manually, and see what the resulting code looks like.
Also note that you need to set .ManualUpdate = False when you are done, otherwise your PivotTable won't update when you are done.

Pivot/Table filter activated through link from other table's cell value

I have a list of items to work through and some items require deeper analysis. Essentially I have two tables. The first shows production results and through filtering column U, I get a list of outliers as in the following image.
Table of outliers
The values displayed in column S then require further analysis for which a pivot chart has been set up. In this chart I filter the particular VRTY number to take a closer look.
Pivot Chart for analysis
Both sheets are contained in the same workbook and I essentially work with two windows open, but to go through the list I have to manually enter every single VRTY value in the pivot filter.
The table of the Pivot chart and the outlier table are not related and data sources are different.
In column S of the outlier table (VRTY) I would ideally turn the values into links that automatically set the pivot filter to this value when clicked.
I am a novice at VBA but from the research I've done this will be the only option - I just haven't come by an instructions how to achieve this particular function.
Instructions/ advice would be highly appreciated.
Sambo: Make sure the orientation of the 'Number' field in the PivotChart's underlying PivotTable is a PageField, and then put the below code in the Sheet code module that corresponds to the sheet where the Outliers PivotTable is:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim pf As PivotField
Dim pf_Slave As PivotField
Set pf = ActiveSheet.PivotTables("PivotTable1").PivotFields("VRTY")
Set pf_Slave = ActiveSheet.PivotTables("PivotTable2").PivotFields("number")
If Not Intersect(Target, pf.DataRange) Is Nothing Then
On Error Resume Next
pf_Slave.CurrentPage = CStr(Target)
On Error GoTo 0
End If
End Sub
Change "PivotTable1" and "PivotTable2" as appropriate.
By "Sheet code module" I mean this type of thing:

Get pivotTable handle from cell in vba

For instance I have a pivot table on every third worksheet that needs to be edited, on the worksheet is 3 pivot tables, I don't actually know the name of the table but I know it lies on $N$2, is there anyway to get the name or handle of the pivot table from the cell?
The Range object has a PivotTable property that returns the object you want
Set pt = MySheet.Range("N2").PivotTable

Resources