Pivot table question

Tigerexcel

Active Member
Joined
Mar 6, 2020
Messages
493
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I thought this would be a pretty simple process but alas... I would like to work out how may staff are under 50 per dept, I'd usually do this through a value filter which in this case is not available via the menu for Age (because it is number data type?). Is there a workaround or does the data require some preparation for this to work?

Book7
ABCDEF
1NameDeptAge
2JimHR62
3HarrySales31Row LabelsCount of Age
4BertAdmin25Admin2
5TomHR33HR3
6HelenSales54Sales3
7KateAdmin31Grand Total8
8NoelHR49
9SteveSales32
Sheet1
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Add Age as a column label as well as in the values. Then you can filter the label for <50.

1601514977365.png
 
Upvote 0
Thanks Peter for the quick response! Yes that works fine.

I notice that it works for Label filter but not for Value filter which is what I would have tried first seeing that Age is a number field.
Also is there any way of making this a little more dynamic for example what if I wanted to see how many are under 40 or between 45 and 55? Could I just reference a cell containing the number 40 etc, instead of having to type the value in the filter?
 
Upvote 0
Also is there any way of making this a little more dynamic for example what if I wanted to see how many are under 40 or between 45 and 55? Could I just reference a cell containing the number 40 etc, instead of having to type the value in the filter?
Assuming that you wanted to keep it as a Pivot Table then you could with vba using a Worksheet_Change event code like this. I have used cell P1 as the age criteria cell.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim rCrit As Range
  Dim myPT As PivotTable
  
  Set rCrit = Range("P1")
  Set myPT = ActiveSheet.PivotTables("PivotTable1")
  If Not Intersect(Target, rCrit) Is Nothing Then
    Application.EnableEvents = False
    myPT.PivotFields("Age").ClearAllFilters
    If Len(rCrit.Value) > 0 And IsNumeric(rCrit.Value) Then myPT.PivotFields("Age").PivotFilters.Add2 Type:=xlCaptionIsLessThan, Value1:=rCrit.Value
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
Thanks again Peter, I have modified your code to reference my Pivot table but it's not changing once I change the cell in P1. I'm a little new to Worksheet_Change event code but can't think of what else I need to do in order to get this to work.
 
Upvote 0
I'm a little new to Worksheet_Change event code ...
Perhaps you have the code in the wrong place? It needs to be in that worksheet's module. To implement ..
1. Right click the Pivot Table sheet's name tab and choose "View Code".
2. Copy and Paste the code into the main right hand pane that opens at step 1.
3. Close the Visual Basic window & test.
(4. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm).)
 
Upvote 0
Perhaps you have the code in the wrong place? It needs to be in that worksheet's module. To implement ..
1. Right click the Pivot Table sheet's name tab and choose "View Code".
2. Copy and Paste the code into the main right hand pane that opens at step 1.
3. Close the Visual Basic window & test.
(4. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm).)
Thanks Peter, all sorted now. I had placed it in the module but hadn't saved it as an xlsm file. Appreciate all the help you've provided on this question.
 
Upvote 0
When I apply this to my actual data, the number of columns displayed is quite large, is there a way to collapse the columns so that only the grand total figures are displayed.
 
Upvote 0
Try

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim rCrit As Range
  Dim myPT As PivotTable
  
  Set rCrit = Range("P1")
  Set myPT = ActiveSheet.PivotTables("PivotTable1")
  If Not Intersect(Target, rCrit) Is Nothing Then
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    With myPT
      .ColumnRange.EntireColumn.Hidden = False
      .PivotFields("Age").ClearAllFilters
      If Len(rCrit.Value) > 0 And IsNumeric(rCrit.Value) Then .PivotFields("Age").PivotFilters.Add2 Type:=xlCaptionIsLessThan, Value1:=rCrit.Value
      .ColumnRange.Resize(, .ColumnRange.Columns.Count - 1).EntireColumn.Hidden = True
    End With
    Application.ScreenUpdating = True
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,845
Members
449,471
Latest member
lachbee

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