MarieThompson
New Member
- Joined
- Apr 11, 2025
- Messages
- 1
- Office Version
- 365
- 2016
- Platform
- 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
- Tried to refresh pivot cache by code
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:
Note: Im using Excel 2016
- 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: