VBA IF a blank or if "(blank") filter the pivot

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
121
So here is what I've been using but when it does not have a blank or (blank) in the pivot table then I get a debug error. How can I get this to work but ti skip if its an error or if it does not exist instead of erroring out

Code:
    ' Filter out blanks on Pivot Tables    
Sheets("Pivot").Activate
    ActiveWorkbook.RefreshAll
    
   Sheets("Pivot").PivotTables("PivotTable5").PivotFields("Legacy Item #"). _
        ClearAllFilters
    With Sheets("Pivot").PivotTables("PivotTable5").PivotFields("Legacy Item #")
        .PivotItems("(blank)").Visible = False
    End With
    Sheets("Pivot").PivotTables("PivotTable1").PivotFields("SOS").ClearAllFilters
    With Sheets("Pivot").PivotTables("PivotTable1").PivotFields("SOS")
        .PivotItems("").Visible = False
        .PivotItems("(blank)").Visible = False
    End With
 

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

JustynaMK

Well-known Member
Joined
Aug 28, 2016
Messages
654
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hi cizzett,

You can use "On Error Resume Next / On Error GoTo 0" statement in order to jump to the next line in case VBA encounters an error.

Code:
[COLOR=#ff8c00]    On Error Resume Next[/COLOR]
        With Sheets("Pivot").PivotTables("PivotTable5").PivotFields("Legacy Item #")
            .PivotItems("(blank)").Visible = False
        End With
[COLOR=#ff8c00]    On Error GoTo 0[/COLOR]

Hope it helps.
 

Watch MrExcel Video

Forum statistics

Threads
1,109,366
Messages
5,528,269
Members
409,813
Latest member
robyrux

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top