VBA Filtering PivotField - Error Unable to get the objects property of the worksheet class

MarieThompson

New Member
Joined
Apr 11, 2025
Messages
1
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I'm a newbie to VBA and trying to use VBA to filter PivotField name 'Zone' on a PivotTable, but I'm getting the 'run time error 1004: unable to get the objects property of the worksheet class' on line 'Set pf = pt.PivotFields("Zone")'. Things I already tried:

- Checked the PivotTable Name 'TrackingTable' and Field name 'Zone' on tab PivotTable Analyze and Field Settings, and it's correct
- Tried to check the field names by code
VBA Code:
Sub CheckFields()
Dim pt As PivotTable
Dim pf As PivotField

Set pt = ActiveSheet.PivotTables("TrackingTable")

For Each pf In pt.PivotFields
Debug.Print ("TrackingTable")
Next
End Sub

- Tried to refresh pivot cache by code


VBA Code:
Sub RefreshAllPivotCaches()

Dim wb As Workbook
Dim lPCs As Long
Dim lPC As Long
Dim lProb As Long

Set wb = Application.ThisWorkbook
lPCs = wb.PivotCaches.Count

For lPC = 1 To lPCs
wb.PivotCaches(lPC).Refresh
If Err.Number <> 0 Then
MsgBox "Could not refresh pivot cache " & lPC _
& vbCrLf _
& "Error: " _
& vbCrLf _
& Err.Description
Err.Clear
lProb = lProb + 1
End If
Next lPC

MsgBox "Refresh is complete. " _
& vbCrLf _
& "Pivot Cache Count: " & lPCs _
& vbCrLf _
& "Failed refreshes: " & lProb

End Sub

But it keeps giving me the same error. Could someone help me? It's a simple problem, but there's something I'm not seeing.

My code:

VBA Code:
Sub FilterDA6()


Dim pt As PivotTable
Dim pf As PivotField
Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Tracking")

Set pt = ws.PivotTables("TrackingTable")
Set pf = pt.PivotFields("Zone")

pf.ClearAllFilters

pf.PivotFilters.Add Type:=xlCaptionEquals, Value1:="DA6"

Set pt = Nothing
Set pf = Nothing
Set ws = Nothing

```

End Sub

Note: Im using Excel 2016
 
Last edited by a moderator:
If that doesn't help, try referring to the index number of the pivot table instead, or put a break point on the problem line and run the code. When it stops, type
?pt.Name
in the immediate window and hit Enter. See if the name that is returned is spelled exactly as you think it should be. I've found that when creating code for some objects, the real name is not the same as what is used in the procedure name.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: VBA Filtering PivotField - Error Unable to get the objects property of the worksheet class
There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

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
Back
Top