VBA code to filter out blank values in a pivot table

dragontbone

New Member
Joined
Sep 28, 2022
Messages
37
Office Version
  1. 365
Platform
  1. Windows
I am attempting to have VBA loop through all of the sheets in my workbook and find a pivot table from several of those sheets located in the same spot for each one. I need it to filter out each instance of (blank) and "" values. However, the code is not filtering out the blanks.


Broker 1
Year
Quarter
Row Labels
(blank)


I can't show any more of this pivot table since it has to do with some sensitive info, but any help would be fantastic.

VBA Code:
Sub RefreshAllPivotTables()
    Application.Calculation = xlAutomatic
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    Application.DisplayAlerts = False
    
    PleaseWait
    DoEvents
    Application.ScreenUpdating = False 
    cleanMasterTable
    
    ' Loop through each worksheet in the workbook
    For Each ws In ThisWorkbook.Worksheets
        ws.Activate 
        
        ' Check if the second word in the sheet name is "Summary"
        Dim sheetNameParts() As String
        sheetNameParts = Split(ws.Name, " ")
        
        If UBound(sheetNameParts) >= 1 Then
            If sheetNameParts(1) = "Summary" Then
                ' Loop through each PivotTable in the worksheet
                For Each pt In ws.PivotTables
                    pt.RefreshTable
                    
                    ' Apply filters and sort if pivot table is in columns G and H starting from row 72
                    If Not Intersect(pt.TableRange2, ws.Range("G72:H" & ws.Rows.Count)) Is Nothing Then
                        On Error Resume Next
                        Set pf = pt.PivotFields("Broker Pd")
                        On Error GoTo 0
                        
                        If Not pf Is Nothing Then
                            With pf
                                .ClearAllFilters
                                On Error Resume Next
                                ' Ensure the "(blank)" item is hidden
                                If .PivotItems("(blank)").Visible Then
                                    .PivotItems("(blank)").Visible = False
                                End If
                                
                                ' Hide any empty or whitespace values
                                For Each pi In .PivotItems
                                    If Trim(pi.Value) = "" Then
                                        pi.Visible = False
                                    End If
                                Next pi
                                On Error GoTo 0
                                
                                ' Sort the items
                                .AutoSort xlDescending, "Broker Pd"
                            End With
                        End If
                    End If
                Next pt
            End If
        End If
    Next ws
    
    HidePleaseWait

    Worksheets("OBK Summary Detail").Activate
    Columns("A:H").Select
    Selection.Columns.AutoFit
    
    Application.ScreenUpdating = True 
    Application.DisplayAlerts = True
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this:

VBA Code:
Sub RefreshAllPivotTables()
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    Dim sheetNameParts() As String
    
    Application.Calculation = xlAutomatic
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    Call PleaseWait
    DoEvents
    Call cleanMasterTable
    
    ' Loop through each worksheet in the workbook
    For Each ws In ThisWorkbook.Worksheets
        ws.Activate
        
        ' Check if the second word in the sheet name is "Summary"
        sheetNameParts = Split(ws.Name, " ")
        
        If UBound(sheetNameParts) >= 1 Then
            If sheetNameParts(1) = "Summary" Then
                ' Loop through each PivotTable in the worksheet
                For Each pt In ws.PivotTables
                    pt.RefreshTable
                    
                    ' Apply filters and sort if pivot table is in columns G and H starting from row 72
                    If Not Intersect(pt.TableRange2, ws.Range("G72:H" & ws.Rows.Count)) Is Nothing Then
                    
                      With pt.PivotFields("Broker Pd")
                          .ClearAllFilters
                          .PivotItems("(blank)").Visible = False
                          ' Sort the items
                          .AutoSort xlDescending, "Broker Pd"
                      End With

                    End If
                Next pt
            End If
        End If
    Next ws
    
    Call HidePleaseWait

    Worksheets("OBK Summary Detail").Activate
    Columns("A:H").Select
    Selection.Columns.AutoFit
    
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub RefreshAllPivotTables()
    Dim ws As Worksheet
    Dim pt As PivotTable
    Dim pf As PivotField
    Dim pi As PivotItem
    Dim sheetNameParts() As String
   
    Application.Calculation = xlAutomatic
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
   
    Call PleaseWait
    DoEvents
    Call cleanMasterTable
   
    ' Loop through each worksheet in the workbook
    For Each ws In ThisWorkbook.Worksheets
        ws.Activate
       
        ' Check if the second word in the sheet name is "Summary"
        sheetNameParts = Split(ws.Name, " ")
       
        If UBound(sheetNameParts) >= 1 Then
            If sheetNameParts(1) = "Summary" Then
                ' Loop through each PivotTable in the worksheet
                For Each pt In ws.PivotTables
                    pt.RefreshTable
                   
                    ' Apply filters and sort if pivot table is in columns G and H starting from row 72
                    If Not Intersect(pt.TableRange2, ws.Range("G72:H" & ws.Rows.Count)) Is Nothing Then
                   
                      With pt.PivotFields("Broker Pd")
                          .ClearAllFilters
                          .PivotItems("(blank)").Visible = False
                          ' Sort the items
                          .AutoSort xlDescending, "Broker Pd"
                      End With

                    End If
                Next pt
            End If
        End If
    Next ws
   
    Call HidePleaseWait

    Worksheets("OBK Summary Detail").Activate
    Columns("A:H").Select
    Selection.Columns.AutoFit
   
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Unfortunately, that did not work. I am not sure if this is relevant or not, but the Broker Pd field is in the Rows section of the pivot table, and not the filters section.
 
Upvote 0

Forum statistics

Threads
1,223,871
Messages
6,175,092
Members
452,612
Latest member
MESTeacher

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