|
Home > Pivot > Calculation > Macros Excel Pivot Table Calculated Field Macros
|

To temporarily remove calculated fields from the pivot table on the worksheet, you can manually uncheck the calculated field boxes, in the Pivot Table Field List.
Later, you can check the boxes again, to put the calculated fields back into the layout.
However, if you record a macro while doing those steps, Excel shows an error message, when you try to run that macro later.

In the recorded code, Excel tries to change the Orentation property for the calculated field, to make it hidden.
ActiveSheet.PivotTables("ptSalesCat") _
.PivotFields("Disc") _
.Orientation = xlHidden
That line of code creates the error, so I had to find a different way to temporarily remove a calculated field.
In this this video, I start by recording a macro to remove a calculated field. Next, I show the error message that appears when that recorded macro runs.
After that, I show my macro that will successfully remove all the calculated fields, without showing an error message.
The sample code is below the video, and in the Excel file that you can get in the download section.
Video Timeline
In the recorded macro, the error occurs at the line that tries to change the Orientation for a calculated field.
To avoid that error, I created a revised macro, that removes all the calculated fields by changing their Visible property to False.
You can copy the code below, and paste it into a code module in your workbook.
Or, you can get the sample file from the download section, and use it to test the code, and see how it works on the sample pivot table.
Sub RemoveALLCalculatedFields()
Dim pt As PivotTable
Dim pf As PivotField
Dim df As PivotField
On Error Resume Next
Set pt = ActiveCell.PivotTable
If pt Is Nothing Then
MsgBox "Select a pivot table cell"
Exit Sub
End If
For Each pf In pt.CalculatedFields
For Each df In pt.DataFields
If df.SourceName = pf.Name Then
With df
.Parent.PivotItems(.Name) _
.Visible = False
End With
Exit For
End If
Next df
Next pf
End Sub
After you add calculated fields or calculated items in a pivot table, you can create a quick list of all the formulas in those calculations.
To create a list of all the formulas in a specific pivot table, you can use the List Formulas command, as shown above.
There is no built-in command that will list the formulas for all of the pivot tables in a workbook, but you can use my List All Formulas macro, to do that.
When you run the ListAllPivotFormulas macro, the code adds a new worksheet to the active workbook.
On that sheet, it lists of all the calculated items and all calculated fields, in all of the active workbook's pivot tables.
The list that the macro creates has 7 columns:

To run the macro, you can use the Excel Macros command, or a worksheet button.
To use the Macros command, follow the steps below:

In some workbooks, I add rounded rectangle shapes on the worksheets, so it's easy for people to run a macro.
Next, I add a caption to each shape, with a brief description of what the "button" does.
Finally, to make the button run a macro, I follow the steps below:

You can copy the code below, and paste it into a code module in your workbook.
Or, you can get the sample file from the download section, and use it to test the code, and see how it works on the sample pivot table.
Sub ListAllPivotFormulas()
'www.contextures.com
'print all the pivot table formulas
'in the active workbook
Dim lRow As Long
Dim wb As Workbook
Dim ws As Worksheet
Dim wsFP As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim cf As Variant 'calculated field
Dim ci As Variant 'calculated item
Dim strSh As String
Dim lPI As Long
On Error Resume Next
Application.DisplayAlerts = False
Set wb = ActiveWorkbook
strSh = "FP_" & Format(Date, "yyyymmdd")
On Error Resume Next
Worksheets(strSh).Delete
On Error GoTo exitHandler
Set wsFP = Worksheets.Add
With wsFP
.Name = strSh
.Columns("A:E").NumberFormat = "@" 'text format
.Range(.Cells(1, 1), .Cells(1, 7)).Value _
= Array("ID", "Sheet", "PivotTable", _
"Type", "Field", "Name", "Formula")
.Rows(1).Font.Bold = True
End With
lRow = 2
For Each ws In wb.Worksheets
If ws.PivotTables.Count > 0 Then
For Each pt In ws.PivotTables
For Each cf In pt.CalculatedFields
wsFP.Range(wsFP.Cells(lRow, 1), _
wsFP.Cells(lRow, 7)).Value _
= Array(lRow - 1, _
ws.Name, pt.Name, _
"Calc Field", , cf.Name, _
" " & cf.Formula)
lRow = lRow + 1
Next cf
For Each pf In pt.PivotFields
On Error Resume Next
lPI = 0
lPI = pf.CalculatedItems.Count
On Error GoTo errHandler
If lPI > 0 Then
For Each ci In pf.CalculatedItems
wsFP.Range(wsFP.Cells(lRow, 1), _
wsFP.Cells(lRow, 7)).Value _
= Array(lRow - 1, _
ws.Name, pt.Name, _
"Calc Item", pf.Name, _
ci.Name, " " & ci.Formula)
lRow = lRow + 1
Next ci
End If
Next pf
Next pt
End If
Next ws
wsFP.Columns("A:G").EntireColumn.AutoFit
exitHandler:
Application.DisplayAlerts = True
Exit Sub
errHandler:
MsgBox "Could not list formulas"
Resume exitHandler
End Sub
Get Monthly Excel Tips!Don't miss my monthly Excel newsletter! You'll get quick tips, article links, and a bit of fun. Add your email, and click Sign Up. Next, when you get my reply, click the Confirm button. I add this step to protect you from spam! |
Calculated Items vs Calculated Fields
Clear Old Items in Pivot Table
Last updated: September 10, 2025 3:56 PM