[Solved] Unable to Get the PivotFields Property of PivotTable Class

The VBA run-time error ‘1004’: “Unable to get the PivotFields property of the PivotTable class” is one of the most common errors when automating Pivot Tables. This error occurs when your code tries to manipulate a field that Excel cannot find or when it cannot correctly identify the Pivot Table itself.

In this article, we will walk you through three causes of this error and provide the exact VBA code solution for each scenario.

Key Takeaways

To solve the “Unable to get the PivotFields property of the PivotTable class” error, here is one simple solution by verifying the field name in VBA.

➤ Click Developer > Visual Basic.
➤ Open a module, insert the following VBA code, and run it.

Sub Fix_WrongFieldName()
Dim pt As PivotTable
Set pt = Worksheets("Pivot 1").PivotTables("PivotTable1")
pt.PivotFields("Product").Orientation = xlRowField
MsgBox "Fixed: Correct PivotField name applied successfully."
End Sub

➤ After running the code with the proper field name, you will not get the error anymore.

overview image

Download Practice Workbook
1

Using a Field Name That Doesn’t Exist

One of the most common reasons for this error is an incorrect field name in your VBA code. When you use the line pt.PivotFields(“FieldName”), Excel looks for a column header in your source data that exactly matches “FieldName”. If the “FieldName” does not match your Pivot Table column header, it will return an error. If the header is “Product” but you write “Productxx“, the code will fail with the ‘1004’ error.

Suppose we have a dataset of some product sales, and the data is converted into a table. Now, we will create a Pivot Table using this table and first apply the incorrect code to display the error. Then, we will use the correct code to resolve the error.

Using a Field Name That Doesn’t Exist

➤ Select the table, go to Insert > PivotTable.

Using a Field Name That Doesn’t Exist

The PivotTable will be created, where the worksheet is named ‘Pivot 1‘ and the PivotTable is named ‘PivotTable1‘.

Using a Field Name That Doesn’t Exist

The Problematic Code:

➤ Go to Developer and click Visual Basic.

Using a Field Name That Doesn’t Exist

➤ To open a new module, click Insert > Module.

Using a Field Name That Doesn’t Exist

➤ Insert the code below and hit Run.

Sub Wrong_FieldName()
Dim pt As PivotTable
Set pt = Worksheets("Pivot 1").PivotTables("PivotTable1")
pt.PivotFields("Productxx").Orientation = xlRowField
End Sub

Using a Field Name That Doesn’t Exist

In the following example, we attempt to set the orientation of a field named “Productxx“, which does not exist in our source data. Thus, we get the “Unable to get the PivotFields property of the PivotTable class” error.

Using a Field Name That Doesn’t Exist

Fix: Correct the Field Name

To fix this, simply correct the field name in the PivotFields property to match the actual column header in your source data, which is “Product“.

➤ Insert a module, paste the code below, and hit Run.

Sub Fix_WrongFieldName()
Dim pt As PivotTable
Set pt = Worksheets("Pivot 1").PivotTables("PivotTable1")
pt.PivotFields("Product").Orientation = xlRowField
MsgBox "Fixed: Correct PivotField name applied successfully."
End Sub

Using a Field Name That Doesn’t Exist

As a result, running the corrected macro will now execute successfully, setting the ‘Product‘ field as a row field.

Using a Field Name That Doesn’t Exist


2

Using the Wrong PivotTable Reference

If you insert an incorrect PivotTable name or worksheet name for the PivotTable you want to work with, you will get the same error. To solve this, make sure the macro code consists of a proper PivotTable and worksheet name.

The Problematic Code:

In this code, our PivotTable is named “PivotTable1“, but when we use “PivotTableX” in the code, we get the error message.

➤ Open a module, paste the code, and hit Run.

Sub Wrong_PivotTableName()
Dim pt As PivotTable
Set pt = Worksheets("Pivot 1").PivotTables("PivotTableX")
pt.PivotFields("Product").Orientation = xlRowField
End Sub

Thus, we get the error message as the pt object indicates a non-existent Pivot Table.

Using the Wrong PivotTable Reference

Fix: Correct the PivotTable Name

The solution is to correct the Pivot Table name that is set for the Pivot Table you are working with.

➤ Insert a module, put the following code, and press  F5  or the Run icon.

Sub Fix_WrongPivotTableName()
Dim pt As PivotTable
Set pt = Worksheets("Pivot 1").PivotTables("PivotTable1")
pt.PivotFields("Product").Orientation = xlRowField
MsgBox "Fixed: Correct PivotTable reference applied successfully."
End Sub

Using the Wrong PivotTable Reference

A message box will appear, ignoring the error, and correctly identify the Pivot Table and apply the field setting.

Using the Wrong PivotTable Reference


3

Selecting a Cell Outside the PivotTable

If your VBA code relies on the ActiveCell.PivotTable property, the code will fail if you click anywhere outside the boundaries of the Pivot Table before running the macro. The ActiveCell then belongs to the Range class, which does not have a PivotTable property.

The Problematic Code:

When your chosen cell is outside the Pivot Table area (e.g., in cell B100), the following code will generate an error.

➤ Open a module, put the following code, and press  F5  or the Run icon.

Sub Wrong_ActiveCellReference()
ActiveCell.PivotTable.PivotFields("Product").Orientation = xlRowField
End Sub

Selecting a Cell Outside the PivotTable

As a result, the error message box will appear as the active cell is not part of the Pivot Table.

Selecting a Cell Outside the PivotTable

Fix: Reference the Pivot Table Object Directly

To solve this, you should never rely on ActiveCell when manipulating Pivot Tables. The best practice is to declare and set a PivotTable object directly, using the sheet and name references.

➤ Create a new module, write the following code, and press  F5  or the Run icon.

Sub Fix_ActiveCellOutsidePivot()
Dim pt As PivotTable
Set pt = Worksheets("Pivot 1").PivotTables("PivotTable1")
pt.PivotFields("Product").Orientation = xlRowField
MsgBox "Fixed: PivotField accessed directly without relying on ActiveCell."
End Sub

Selecting a Cell Outside the PivotTable

Finally, we will get a message box confirming that the code is running properly. By referencing the Pivot Table directly, the code will run successfully every time, regardless of where the cursor is placed on the sheet.

Selecting a Cell Outside the PivotTable


Frequently Asked Questions

Does the timing of PivotTable creation affect this error?

Absolutely. If you try to access PivotFields immediately after creating a PivotTable, the PivotCache may not be ready. Adding a small delay or separating the creation and modification steps helps.

How can I debug this error quickly?

Step through your VBA code using  F8  and check values for PivotTable names, field names, and ranges in the Immediate Window (Ctrl+G). This usually pinpoints the exact issue.

Is this error specific to the PivotTable Class only?

No. A similar error can also happen with the Worksheet or Range class if you mistakenly use them with PivotFields. But for the PivotTable class, it directly indicates a problem with field referencing.


Concluding Words

Above, we have explored three common reasons behind the “Unable to Get the PivotFields Property of the PivotTable Class” error in VBA. While using a non-existent field name is the most frequent cause, an incorrect PivotTable reference or selecting a cell outside the PivotTable can also trigger this error. By carefully checking these points, you can easily troubleshoot and fix the issue. If you have any further questions, feel free to let us know in the comments section below.

ExcelInsider Team
We will be happy to hear your thoughts

Leave a reply

Excel Insider
Logo