Home > Pivot > Calculation > Macros

Excel Pivot Table Calculated Field Macros

Debra Dalgleish - Contextures

In a pivot table, you can create calculated fields, using your own custom formulas. See how to use macros with calculated fields, and avoid errors when running those macros.

calculated field macro errorUnable to set the Orientation property of the PivotField class

1) Remove Pivot Table Calculated Field

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.

1.1) Macro Recorder Problem

However, if you record a macro while doing those steps, Excel shows an error message, when you try to run that macro later.

  • "Run-time error '1004': Unable to set the Orientation property of the PivotField class"

calculated field macro errorUnable to set the Orientation property of the PivotField class

1.2) VBA Code Problem

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.

1.3) Video: Macro to Remove 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

  • 0:00 Record a Macro
  • 0:59 Test the Macro
  • 1:28 View the Recorded Code
  • 2:58 Macro to Remove Calculated Fields
  • 4:32 Test the New Macro

1.4) Revised Macro to Remove Calculated Field

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.

  • Thanks to Rory Archibald, for suggesting this solution.

Macro Code: Remove Calculated Fields

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.

  • Note: Before running the macro, you must have a pivot table cell selected. Only the selected pivot table is affected.
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

2) Create a List of Pivot Table Formulas

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.

  • Manual steps: In the short video below, I show how to make that pivot table formula list manually, and there are written steps on the Pivot Table Calculated Field page.
  • Save time with a macro: Below the video, there's my time-saving macro that will automate this task for you. It lists all the formulas, for all of the pivot tables, in a workbook.

2.1) Macro Lists All Formulas For All Pivot Tables

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.

2.2) What the Macro Does

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:

  1. ID: Sequential numbers for the pivot table formulas
  2. Sheet: Worksheet name where the pivot table is located
  3. Type: Calculated Field formula or Calculated Item formula
  4. Field: For Calculated Items, the related pivot field name
  5. Name: Name of the pivot table formula
  6. Formula: Formula used in the calculated item or calculated field

list of all formulas for all pivot tables in workbook

2.3) How to Run the Macro

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

2.3.1) Macros Command

To use the Macros command, follow the steps below:

  • On the Excel Ribbon, go to the View tab
  • At the right end of the tab, in the Macros group, click the top part of the Macros button
  • When the Macros dialog box opens, click on the x macro name, to select it
  • At the right, click the Run button, to run the selected macro

run a macro with Macros command

2.3.2) Macro Button on Worksheet

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.

  • In the sample file, there's a button on the PolicyReport sheet, with the caption "List Pivot Formulas"

Finally, to make the button run a macro, I follow the steps below:

  • Right-click the button, and in the pop-up menu, click the Assign Macro command
  • In the Assign Macro dialog box, a default macro name is in the box at the top
  • Other macros in the workbook are listed below.
  • Click on the ListAllPivotFormulas macro, to select it
  • To assign the selected macro, click the OK button

formula list

2.4) Macro Code: List All Formulas for All Pivot Tables

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

3) Download the Sample Files

  1. Macro-Formula List: Sample file with the code to create a list of all pivot table formulas. The file is zipped, and is in xlsm format. The file contains macros, so enable them to test the code.
  2. Macro-Remove CF: Sample file with the code to remove all Calculated Fields. The file is zipped, and is in xlsm format. The file contains macros, so enable them to test the code.

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!

More Pivot Table Tutorials

Calculated Items vs Calculated Fields

Pivot Table Calculated Fields

Calculated Field - Count

PIVOTBY Function

Calculated Items

FAQs - Pivot Tables

Pivot Table Introduction

Grouping Data

Multiple Consolidation Ranges

Running Totals

Summary Functions

Clear Old Items in Pivot Table

 

 

Last updated: September 10, 2025 3:56 PM