pivot table question

olympiac

Board Regular
Joined
Sep 26, 2010
Messages
158
I have a huge amount of data filtered in a pivot table. The xample below is simplified to understand the feasibility of my request.
Heading:name Heading:animal
name 1 cat
name 2 cat
name 3 cat
name 4 cat
name 5 cat
name 6 cat
name 7 dog
name 8 dog
name 9 dog
name 10 dog

Is it possible to filter the pivot table by "cat" (column label) and keep all the 10 names (Raw label)? From name 7 to 10 I need empty value in this example.
Is it feasible?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Is the example you show your data source, or what your pivot table looks like before it is filtered by cat?

If it's the latter with cat and dog as column labels then it would look more like the first images below.
Excel Workbook
FGHI
2Before Filter
3Column Labels
4Row LabelsCatDogGrand Total
5Name 11212
6Name 21414
7Name 31616
8Name 41818
9Name 52020
10Name 62222
11Name 72424
12Name 82626
13Name 92828
14Name 103030
15Grand Total12684210
Sheet


To display as shown in the second image after filtering...
  • Go to Field Settings for the "Name" field
  • On Layout & Print Tab
  • Check "Show items with no data"
Excel Workbook
FGHI
2After Filter
3Column Labels
4Row LabelsCatGrand Total
5Name 11212
6Name 21414
7Name 31616
8Name 41818
9Name 52020
10Name 62222
11Name 72424
12Name 8
13Name 9
14Name 10
15Grand Total126126
Sheet
 
Upvote 0
Thanks to the feature "Show items with no data", the pivot table displays now all the names in the table
What about if we want to filter this list and show only specific names by applying a filter?
Let's suppose that there is an additional field related to a country and we want to show all the people in a specific country that have got a cat.In this case, some names that do not belong to that country should not appear in the pivot.
Is it possible to apply a filter to the feature "Show items with no data"? If not, is there an alternative?
Thanks
 
Upvote 0
Thanks to the feature "Show items with no data", the pivot table displays now all the names in the table
What about if we want to filter this list and show only specific names by applying a filter?
Let's suppose that there is an additional field related to a country and we want to show all the people in a specific country that have got a cat.In this case, some names that do not belong to that country should not appear in the pivot.
Is it possible to apply a filter to the feature "Show items with no data"? If not, is there an alternative?
Thanks

That's an interesting question. Let's say you have the Source Data below, and you want to display a list of names with Cats in Ireland.
Excel Workbook
ABCD
1Source Data
2NameCountryAnimalValue
3Name 1IrelandCat1
4Name 2IrelandCat2
5Name 3IrelandDog3
6Name 4IrelandDog4
7Name 5FinlandCat5
8Name 6FinlandCat6
9Name 7NorwayCat7
10Name 8NorwayDog8
11
Sheet



If you filter for Cats and Ireland, you'll get this result with "Show items with no data" Checked for Names.
This includes people who are not in Ireland as you pointed out.
Excel Workbook
GHI
5Animal
6CountryNameCat
7IrelandName 11
8Name 22
9Name 3
10Name 4
11Name 5
12Name 6
13Name 7
14Name 8
15Grand Total3
Sheet



If you filter for Cats and Ireland, you'll get this result with "Show items with no data" Unchecked for Names.
This excludes some people who are in Ireland, but don't have a cat (and your OP question was how to show these names).
Excel Workbook
GHI
5Animal
6CountryNameCat
7IrelandName 11
8Name 22
9Grand Total3
Sheet


I don't know of a way to display this "desired result" in the PivotTable using only filters and settings from that datasource.
Excel Workbook
GHI
5Animal
6CountryNameCat
7IrelandName 11
8Name 22
9Name 30
10Name 40
11Grand Total3
Sheet



One work-around which I used to produce the desired result image is to modify datasource to include records for 0 Cats and 0 Dogs.
It's not an ideal solution, but it would allow you to get the display you want. If you decide to use that work around, it would probably be best to have a separate field in your datasource for each type of animal instead of a single field for all animals.
 
Upvote 0
Smart solution however I cannot apply it to my data source because I have got more than 50 different items (let's say 50 different anilmals in our example)
Another work around is to create a pivot table with only one field (Raw label) where all the animals are listed and a main Page filter to choose the Country. The idea is to synchronise this pivot table with the main pivot table. By doing so, if I select the country "Ireland" and the animal "Cat" in the main pivot table, the second pivot will display all the people that have got a "cat" in Ireland.
Next step is to apply a Vlookup in a column next the main Pivot table (remember that the feature Show items with no data" is applied). By doing so, I can see the people that are listed in the second pivot table.
Next step is to call a VBA code to hide the rows with (iserror) as a result of the Vlookup.
This work around works fine but the problem is that unfortunately when I select multiple items (multiple countries) in the main pivot table, the second pivot table does not synchronise.
Do you know how to Select multiple items in one of the pivot table's page fields and get another table synchronised?
 
Upvote 0
Do you know how to Select multiple items in one of the pivot table's page fields and get another table synchronised?

You could try the code below. Each time there is a change to PivotTable1,
it will synch the Visible state of each item in your Country Field in PivotTable2.

Copy this into the code space for the sheet that has PivotTable1

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    With ActiveSheet
        If Intersect(Target, .PivotTables("PivotTable1") _
            .TableRange2) Is Nothing Then Exit Sub
        
        On Error GoTo CleanUp
        Application.EnableEvents = False
        Call Synch_PT_Filters( _
            PT1:=.PivotTables("PivotTable1"), _
            PT2:=.PivotTables("PivotTable2"), _
            sField:="Country")
    End With
CleanUp:
    Application.EnableEvents = True
End Sub

Copy these two functions into a standard module.
Code:
Public Function Synch_PT_Filters(PT1 As PivotTable, PT2 As PivotTable, _
    sField As String)
    Dim sVisibleItems() As String
    Dim pviItem As PivotItem
    Dim i As Long
    
    '---make array of visible items in PT1
    With PT1.PivotFields(sField)
        If .Orientation = xlPageField And _
            .EnableMultiplePageItems = False Then
                ReDim sVisibleItems(1)
                sVisibleItems(0) = .CurrentPage
        Else
            For Each pviItem In .PivotItems
                If pviItem.Visible Then
                    i = i + 1
                    ReDim Preserve sVisibleItems(i)
                    sVisibleItems(i - 1) = pviItem
                End If
            Next
        End If
    End With
   
    '---make visible items in PT2 match PT1
    With PT2.PivotFields(sField)
        If .Orientation = xlPageField Then .EnableMultiplePageItems = True
        If sVisibleItems(0) = "(All)" Then
            For i = 1 To .PivotItems.Count
                If Not .PivotItems(i).Visible Then _
                    .PivotItems(i).Visible = True
            Next i
        Else  'ensure 1 item visible
            .PivotItems(sVisibleItems(0)).Visible = True
            For i = 1 To .PivotItems.Count
                If .PivotItems(i).Visible <> _
                    isMember(.PivotItems(i), sVisibleItems) Then
                    .PivotItems(i).Visible = Not (.PivotItems(i).Visible)
                End If
            Next i
        End If
    End With
End Function

Public Function isMember(sItem As String, _
        ByRef sArr() As String) As Boolean
    Dim i As Long
    For i = LBound(sArr) To UBound(sArr)
        If sArr(i) = sItem Then
            isMember = True
            Exit Function
        End If
    Next i
    isMember = False
End Function

You might need more than a standard Vlookup formula in a column next the main Pivot table.
If you are going to have multiple countries selected, you'll probably need a formula that can find matches for two criteria.

Good luck!
 
Last edited:
Upvote 0
The code works perfectly when PT1 and PT2 are in the same sheet.
How can I modify it to make workable when PT2 is in another Sheet?
 
Upvote 0
Resolved!

Set PT2 = Sheets("Sheet2").PivotTables("PivotTable2")
Many thanks for your support and your suggestions.
 
Upvote 0
When referencing the PivotTables from the Worksheet_Change event code, just include the sheet name for PT2 with the reference.

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    With ActiveSheet
        If Intersect(Target, .PivotTables("PivotTable1") _
            .TableRange2) Is Nothing Then Exit Sub
        
        On Error GoTo CleanUp
        Application.EnableEvents = False
        Call Synch_PT_Filters( _
            PT1:=.PivotTables("PivotTable1"), _
            PT2:=Sheets("MyOtherSheet").PivotTables("PivotTable2"), _
            sField:="Country")
    End With
CleanUp:
    Application.EnableEvents = True
End Sub
 
Upvote 0
I know this is an old post, so hopefully I'm ok dragging it up (sorry if not...)

I'm trying to implement this code with 4 filter variables. So basically I have 4 "sField" variables. I'm sure the implementation isn't anything too complex, but I have close to zero VBA experience. Can anyone help with how I'd set this up so that 4 filters would sync on multiple pivot tables?

Thanks in advance!
John
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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