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
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try...

VBA Code:
    With field
   
        .ClearAllFilters
   
        On Error Resume Next
        .CurrentPage = cat
        On Error GoTo 0
   
    End With

Or, to display a message when the pivot item doesn't exist, try...

VBA Code:
    With field
   
        .ClearAllFilters
   
        On Error Resume Next
        .CurrentPage = cat
        If Err.Number <> 0 Then
            MsgBox "'" & cat & "' not found.", vbInformation
        End If
        On Error GoTo 0
   
    End With

Note that you shouldn't need to refresh the pivot table after assigning a value to CurrentPage. However, when you do want to refresh your pivot table, you don't need the With/End With statement. You can simply use the following line of code..

VBA Code:
pt.RefreshTable

Hope this helps!
 
Upvote 0
Domenic,

Thank you very much for this fix and it totally works! Would you mind assisting with a minor tweak to it? I just realized that when a location is selected but there is no data for it, the pivot table will then select all with this version of the error handling. Can it be tweaked where it would select either the "" or "(blank)"?

Basically a user selects a location via a drop down list which then triggers this filter. The pivot will then populate data but if there shouldn't be any for a location, I don't want it to show all.

Also thank you for the tip on the pt.RefreshTable!

Hope this makes sense and thanks again for your help!
 
Upvote 0
In that case, try the following instead...

VBA Code:
    Dim PvtItm As PivotItem
    
    With field
    
        .ClearAllFilters
    
        On Error Resume Next
        Set PvtItm = .PivotItems(cat)
        If Err.Number = 0 Then
            .CurrentPage = cat
        Else
            .CurrentPage = "(blank)"
        End If
        On Error GoTo 0
  
    End With

Hope this helps!
 
Upvote 0
It doesn't seem to want to do the else part

When I select a location that is valid it will select it correctly but when a location that is invalid is selected it still shows all. I tried flipping the cat and "(blank)" to test and when an invalid selection is selected it does change to "(blank)" but when a valid selection is selected it does not match the pivotitem but instead still selects "(blank)"

Sorry for the troubles but again thank you for your assistance!
 
Upvote 0
Does (blank) appear in the dropdown for your page field?

If you enter the following line of code in the Immediate Window (Visual Basic Editor >> View >> Immediate Window or Ctrl+G), does it update the pivot table accordingly? Or do you get an error?

VBA Code:
? Worksheets("MIR Pivot").PivotTables("PivotTable1").PivotFields("PropertyName").CurrentPage = "(blank)"
 
Upvote 0
No idea why but it does not update the pivot accordingly.....everything looks correct and "(blank)" is an option

Screenshot (2).png
 
Upvote 0
If you enter the following line of code in the Immediate Window (Visual Basic Editor >> View >> Immediate Window or Ctrl+G), does it update the pivot table accordingly? Or do you get an error?

? Worksheets("MIR Pivot").PivotTables("PivotTable1").PivotFields("PropertyName").CurrentPage = "(blank)"

When you enter the above line in the Immediate Window, do you get an error? If so, which error do you get?
 
Upvote 0
When you enter the above line in the Immediate Window, do you get an error? If so, which error do you get?
Sorry there are moments where i'm a complete moron.....upon hitting enter it shows false
Capture.PNG
 
Upvote 0
Sorry, I mistakenly added a question mark (?) at the beginning of the code. It should be...

VBA Code:
Worksheets("MIR Pivot").PivotTables("PivotTable1").PivotFields("PropertyName").CurrentPage = "(blank)"
 
Upvote 0

Forum statistics

Threads
1,214,552
Messages
6,120,172
Members
448,948
Latest member
spamiki

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