VBA Excel - Update all Pivottables in worksheet adding a measure

Ansieb

New Member
Joined
Jun 29, 2018
Messages
1
I have tried to create a macro to add a measure to all the pivot tables in my workbook but cannot get the
syntax correct.
We are running on Microsoft Office Home and Business 2016+
I have managed to add a measure to all the pivot Charts. See macro code attached
:ROFLMAO:

Sub ChangePivot()
' CHANGE AlL PIVOTS IN THE WORKSHEETS
NewCost = Worksheets("Current - Group and Company").Range("T4").Value
Dim ws As Worksheet, pc As ChartObject
For Each ws In ThisWorkbook.Worksheets
For Each pc In ws.ChartObjects
MsgBox pc.Chart.Name & String$(2, vbLf) & pc.Name, pc.ProtectChartObject

For Each CBField In pc.Chart.PivotLayout.PivotTable.CubeFields
If CBField.CubeFieldType = xlMeasure Then
CBField.Orientation = xlHidden

End If
Next CBField

Select Case NewCost
Case "Standard"

pc.Chart.PivotLayout.PivotTable.AddDataField pc.Chart.PivotLayout. _
PivotTable.CubeFields("[Measures].[Total_Std_Cost]")

Case "Average"

pc.Chart.PivotLayout.PivotTable.AddDataField pc.Chart.PivotLayout. _
PivotTable.CubeFields("[Measures].[Total_Average_Cost]")

Case "FIFO"

pc.Chart.PivotLayout.PivotTable.AddDataField pc.Chart.PivotLayout. _
PivotTable.CubeFields("[Measures].[Total_FIFO_Cost]")
End Select
Next
Next
'Sub foo()
'Dim sh As Object, pc As Chart, pco As ChartObject
' For Each sh In ThisWorkbook.Sheets
' For Each pco In sh.ChartObjects
' MsgBox pco.Chart.Name & String$(2, vbLf) & pco.Name
'Next
' Next
' For Each pc In ThisWorkbook.Charts
' MsgBox pc.Name
' Next
'End Sub
'Dim sh As Object, pc As Chart, pco As ChartObject
'For Each objCubeFld In ActiveChart.PivotLayout.PivotTable.CubeFields
'ACTIVE CHART
'For Each CBField In ActiveChart.PivotLayout.PivotTable.CubeFields
'If CBField.CubeFieldType = xlMeasure Then
'CBField.Orientation = xlHidden
'End If
'Next CBField
'Select Case NewCost
'Case "Standard"

' ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
' PivotTable.CubeFields("[Measures].[Total_Std_Cost]")
'Case "Average"

'ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
'PivotTable.CubeFields("[Measures].[Total_Ave_Cost]")
'Case "FIFO"
'ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
'PivotTable.CubeFields("[Measures].[Total_FIFO_Cost]")
'End Select

'ACTIVE CHART
End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Watch MrExcel Video

Forum statistics

Threads
1,129,680
Messages
5,637,749
Members
416,982
Latest member
lisam77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top