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
 
Nah that's my moronic butt not thinking of that too :p

Code works as expected and even tried other options as well.
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Okay that's great, glad you were able to sort it out.

Cheers!
 
Upvote 0
Sorry not exactly...

In this part of the code, the else half of it does not seem to work...I don't think I explained it very well in my initial assessment but the first half works in that it will select the cat when it is valid but if the selection is invalid (location that does not exist yet), it will still select all and not do the else "(blank)"

VBA Code:
    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
 
Upvote 0
When a selection is invalid, it should execute the else statement. If the pivot table doesn't update accordingly, it would suggest the pivot item doesn't exist. But I know that earlier in this thread you showed that it indeed exist. If you run that line of code in the Immediate Window, does that pivot table update? Or do you get an error? If you get an error, which error do you get?
 
Upvote 0
When a selection is invalid, it should execute the else statement. If the pivot table doesn't update accordingly, it would suggest the pivot item doesn't exist. But I know that earlier in this thread you showed that it indeed exist. If you run that line of code in the Immediate Window, does that pivot table update? Or do you get an error? If you get an error, which error do you get?
I get:

Compile error:

End With without With
 
Upvote 0
It's that one line of code that you should have entered in the Immediate Window, not all of the code. Just for clarity, enter the following line in the Immediate Window (Ctrl+G), and press ENTER. What do you get? Do you get an error? If so, which one?

VBA Code:
Worksheets("MIR Pivot").PivotTables("PivotTable1").PivotFields("PropertyName").CurrentPage = "(blank)"
 
Upvote 0
Oops my fault....that line gives no errors and does what it is expected to do
 
Upvote 0
Just curious what is the PvtItm portion of the code supposed to do? All the other Dim's are used in some capacity in the rest of the code except this one.
 
Upvote 0
The PvtItm portion is there simply to check whether that pivot item exists. If it does, the CurrentPage is set to Cat. Otherwise, it's set to (blank).

Can you post the exact and complete code that you're using?
 
Upvote 0
ahhhh gotcha....thanks for explaining that to me

This is the complete code

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
If Intersect(Target, Range("G12:I13")) Is Nothing Then Exit Sub
        
    Dim pt As PivotTable
    Dim field As PivotField
    Dim res As PivotField
    Dim PvtItm As PivotItem
    Dim cat As String
    Dim LR As Long
    Dim pt2 As PivotTable
    Dim field2 As PivotField
    Dim res2 As PivotField
    Dim PvtItm2 As PivotItem
    Dim cat2 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 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
    
    pt.RefreshTable
    
    Set pt2 = Worksheets("GL R Pivot").PivotTables("PivotTable1")
    Set field2 = pt2.PivotFields("Property Name 2")
    Set res2 = pt2.PivotFields("Person/Description")
    cat2 = Worksheets("GL R Pivot").Range("G2").Value

    With field2
        .ClearAllFilters
        
        On Error Resume Next
        If Err.Number = 0 Then
            .CurrentPage = cat2
        Else
            .CurrentPage = ""
        End If
        On Error GoTo 0

    End With
    
    pt2.RefreshTable
    
    LR = Worksheets("MIR Pivot").Range("A" & Rows.Count).End(xlUp).Row - 4
        
    Sheets("CRC Report").Range("A18:I26").ClearContents
    Range("A17").Formula = "=IF($D17="""","""",XLOOKUP($D17,'MIR Mod'!$F$12:$F$500,'MIR Mod'!$A$12:$A$500,""""))"
    Range("B17").Formula = "=IF($D17="""","""",VLOOKUP($D17,'MIR Mod'!$F$12:$Z$500,19,FALSE))"
    Range("C17").Formula = "=IF($D17="""","""",XLOOKUP($D17,'MIR Mod'!$F$12:$F$500,'MIR Mod'!$D$12:$D$500,""""))"
    Range("D17").Formula = "=IF(ISBLANK('MIR Pivot'!A5),"""",'MIR Pivot'!A5)"
    Range("E17").Formula = "=IF($D17="""","""",VLOOKUP($D17,'MIR Mod'!$F$12:$Z$500,20,FALSE))"
    Range("F17").Formula = "=IF($D17="""","""",VLOOKUP($D17,'MIR Mod'!$F$12:$Z$500,21,FALSE))"
    Range("G17").Formula = "=IF($D17="""",0,VLOOKUP($D17,'MIR Mod'!$F$12:$Z$500,8,FALSE))"
    Range("H17").Formula = "=IF($D17="""",0,VLOOKUP($D17,'MIR Mod'!$F$12:$Z$500,9,FALSE))"
    Range("I17").Formula = "=IF($D17="""",0,VLOOKUP($D17,'MIR Mod'!$F$12:$Z$500,15,FALSE))"
    Range("V17").Formula = "=IF(ISNA(VLOOKUP($D17,'MIR Mod'!$F$12:$AA$500,22,FALSE)),"""",IF(VLOOKUP($D17,'MIR Mod'!$F$12:$AA$500,22,FALSE)=""x"",""Special"",""""))"
    Sheets("CRC Report").Range("A17:I17").Copy Range("A17:I" & LR + 16)
    Sheets("CRC Report").Range("V17").Copy Range("V17:V" & LR + 16)
    Range("D" & LR + 17).Formula = "=IF(ISBLANK('GL R Pivot'!A5),"""",'GL R Pivot'!A5)"
    Range("I" & LR + 17).Formula = "=IF(ISBLANK('GL R Pivot'!B5),,'GL R Pivot'!B5)"
                
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

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