+ Reply to Thread
Results 1 to 9 of 9

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

  1. #1
    Registered User
    Join Date
    04-11-2025
    Location
    US
    MS-Off Ver
    2016
    Posts
    9

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

    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

    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


    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:

    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

  2. #2
    Forum Contributor
    Join Date
    03-05-2025
    Location
    Deutschland
    MS-Off Ver
    MS Office Home & Business 2024
    Posts
    221

    Re: VBA Filtering PivotField - Error Unable to get the objects property of the worksheet c

    Hi Diana,

    what about sample file?

    BR, Alexandra

  3. #3
    Registered User
    Join Date
    04-11-2025
    Location
    US
    MS-Off Ver
    2016
    Posts
    9

    Re: VBA Filtering PivotField - Error Unable to get the objects property of the worksheet c

    Hi Alexandra.
    Here is an example file.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    03-05-2025
    Location
    Deutschland
    MS-Off Ver
    MS Office Home & Business 2024
    Posts
    221

    Re: VBA Filtering PivotField - Error Unable to get the objects property of the worksheet c

    Hi Diana,

    you code is OK:

    Sub FilterDA6_2()
    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
    But you pivot was a little bit uncorrect builded, so i build a new one and now it´s works!

    BR, Alexandra
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    04-11-2025
    Location
    US
    MS-Off Ver
    2016
    Posts
    9

    Re: VBA Filtering PivotField - Error Unable to get the objects property of the worksheet c

    Alexandra, thank you very much for your attention, but even in your file when running the macro "Sub FilterDA6_2()", I still get the "run time error 1004 unable to get the pivotfields property of the pivottable class" on the line: Set pf = pt.PivotFields("Zone"). Could it be an error related to the cache?

  6. #6
    Forum Contributor
    Join Date
    03-05-2025
    Location
    Deutschland
    MS-Off Ver
    MS Office Home & Business 2024
    Posts
    221

    Re: VBA Filtering PivotField - Error Unable to get the objects property of the worksheet c

    Hi Diana,

    so if you open my file and you run directly the macro in my file without change anything, the problem is stil there?

    br, Alexandra

  7. #7
    Valued Forum Contributor MikeVol's Avatar
    Join Date
    12-30-2020
    Location
    Odessa / Ukraine
    MS-Off Ver
    MSO Prof Plus 2021 x64 (En)
    Posts
    514

    Re: VBA Filtering PivotField - Error Unable to get the objects property of the worksheet c

    Hello @Diana Treptow
    Tested the file from @cysu1, the code worked without a single error. The code works!
    NOTE: As the original poster/owner, only you can mark your thread as SOLVED (Thread Tools above Post #1).
    You can say "Thanks" in your thread to everyone who offered to help you.
    You can also reward them by clicking * "Add Reputation" under their username on the left.
    With Regards, MikeVol.

  8. #8
    Forum Contributor
    Join Date
    03-05-2025
    Location
    Deutschland
    MS-Off Ver
    MS Office Home & Business 2024
    Posts
    221

    Re: VBA Filtering PivotField - Error Unable to get the objects property of the worksheet c

    Thanks MikeVol!

  9. #9
    Registered User
    Join Date
    04-11-2025
    Location
    US
    MS-Off Ver
    2016
    Posts
    9

    Re: VBA Filtering PivotField - Error Unable to get the objects property of the worksheet c

    Yes, even after downloading the file and not changing anything on table/pivottable or VBA, I still get the same error, according to print.
    But I appreciate the help. Thanks also MikeVol, for the feedback.
    Attachment 891860

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro error creating Pivot - Unable to get the LabelRange property of the PivotField class
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-10-2024, 10:56 PM
  2. Error 1004 Unable to set the CurrentPage property of the PivotField class
    By Zadira in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-25-2020, 03:13 PM
  3. error msg : unable to get the PivotItems property of the PivotField class
    By Kim75 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-12-2015, 03:56 PM
  4. Getting "Unable to set CurrentPage property of PivotField class" error
    By jsweez24 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-06-2015, 03:03 PM
  5. RUN –TIME ERROR ‘1004’: Unable to set the currentpage property of the pivotfield class
    By saudi_red_neck in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-13-2014, 02:54 AM
  6. runtime 1004 error 'Unable to get the Pivotitems property of the Pivotfield class'
    By DavidBW in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-01-2012, 10:36 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1