Error handling pivot field VBA

muhleebbin

Active Member
Joined
Sep 30, 2017
Messages
252
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi!

I have the following code however on occasion I do run into an error when the pivot field (cat) that the code is looking for does not exist. Can someone help with the error handling code that would eliminate this?

Thank you in advance!

VBA Code:
    Dim pt As PivotTable
    Dim field As PivotField
    Dim res As PivotField
    Dim cat As String

    Application.ScreenUpdating = False
    
    Set pt = Worksheets("MIR Pivot").PivotTables("PivotTable1")
    Set field = pt.PivotFields("PropertyName")
    Set res = pt.PivotFields("Resident")
    cat = Worksheets("MIR Pivot").Range("G2").Value

    With pt
        field.ClearAllFilters
        field.CurrentPage = cat
        pt.RefreshTable
    End With
 
It looks fine, so I've re-written it so that the error handling is done slightly differently. Maybe it might provide us with some clue as to what is happening. So try this instead...

VBA Code:
    With field
        .ClearAllFilters
        
        On Error Resume Next
        Set PvtItm = .PivotItems(cat)
        On Error GoTo 0
        
        If Not PvtItm Is Nothing Then
            .CurrentPage = cat
        Else
            .CurrentPage = "(blank)"
        End If

    End With

Does it work as expected? Or does it give you some error? If so which one and on which line?
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
It highlights .CurrentPage = cat after this

Capture.PNG
 
Upvote 0
Okay, so when that happens, move your cursor over the variable cat. What does it display? Does the value that is displayed appear in the drop-down for your page field?

Actually, hold on a second, you're probably getting that error because With field is not referencing a page field. Can you please confirm whether in fact it is a page field?
 
Upvote 0
Hovering over cat it does say cat = "Vancouver" which is not a drop down option at that time....the more recent data does have Vancouver data

The With field currently references to the PivotField "PropertyName" (field = "PropertyName" when hovered over the with statement)
 
Upvote 0
Then simply refresh your pivot table before setting the current page for your page field. So maybe like this...

VBA Code:
    pt.RefreshTable
    
    With field
    
        .ClearAllFilters
    
        On Error Resume Next
        .CurrentPage = cat
        If Err.Number <> 0 Then
            .CurrentPage = "(blank)"
        End If
        On Error GoTo 0
  
    End With

Does this help
 
Upvote 0
Solution
This worked!!!

Thank you so very much for your help Domenic!
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,858
Members
449,051
Latest member
excelquestion515

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