Cannot count visible items in filtered pivot table correctly

whitehawk81

Board Regular
Joined
Sep 4, 2016
Messages
66
Hi,
I have an issue with pivot tables, that's bugging me. I cannot get the correct count of visible items in pivot tables after changing the filters via VBA.
Here is the code, that should grab every pivot table that has more than 1 row in the data body range:
Code:
Sub changePTfilter(ws As Worksheet, sID As Long)
Dim pt As PivotTable
Dim pf As PivotField
Dim ptName As String
Dim i, n As Long


n = ws.Cells(Rows.Count, 1).End(xlUp).row


Call TU_Start
On Error Resume Next


For i = 1 To ws.PivotTables.Count
ptName = "SolPT" & i
Set pt = ws.PivotTables(ptName)


pt.ManualUpdate = True


Set pf = pt.PivotFields("Solution")
    With pf
        .ClearAllFilters
        .CurrentPage = sID
    End With


    If i < 7 And pf.CurrentPage <> "(All)" Then
        If pt.DataBodyRange.Count > 1 Then    '--- this is the problematic part
            ws.Cells(n, 1).Value = i
            n = n + 1
        End If
    End If


Set pf = Nothing
    With pt
        .PivotCache.Refresh
    End With


pt.ManualUpdate = False


Next i


Call TU_End


End Sub

The closest I got so far is when I looped through all visible items in the pivot field, but that also counted the items, that were filtered out:
Code:
For Each pf In pt.RowFields          
Debug.Print pf.Name & ": " & pf.VisibleItems.Count
    Next
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
I actually managed to solve it by creating a separate subroutine for the loop after the pivot tables have been updated. Maybe not the most elegant way, but works.
Code:
Sub listUsedCharts(ws As Worksheet, pt As PivotTable, pf As PivotField)
Dim i, n As Long
Dim ptName As String


n = ws.Cells(Rows.Count, 1).End(xlUp).row


For i = 1 To 6
ptName = "SolPT" & i
Set pt = ws.PivotTables(ptName)
Set pf = pt.PivotFields("Solution")


    If pf.CurrentPage <> "(All)" Then
        If pt.DataBodyRange.Count > 1 Then
            ws.Cells(n, 1).Value = i
            n = n + 1
        End If
    End If
Next i


End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,975
Members
449,200
Latest member
Jamil ahmed

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