Filter Inside Pivot table data values

Aswinraj

Board Regular
Joined
Dec 10, 2015
Messages
65
Hello,
@MARK858 - Is it possible for you to help me on this please.

I have a Pivot table, where i need to Select Blank Column next to last row of Pivot table and Put Filter. I tried some coding but i am getting an error. Please help to rectify.
Steps:
1. select Row from a Blank Column (Here it is "X") next to Last row of Pivot Table till "Column A" (As per Screen shot K to A)
2. Right Click "Blank Column (Here it is "X") next to Last Row of Pivot table
3. Choose "Filter" then "Filter by Selected Cell's Icon"
4. "Select All" in Filter

VBA Code:
LastCl = Rows(9).Find("*", , xlValues, , xlByColumns, xlPrevious).Select
'LastCl = ActiveCell.Column
FilterRange = "A9:" & LastCl
Range(FilterRange).Select
      'Below Lines are from record macro, i changed it to dynamic since column range may vary
    FilterRange = Range(Selection, Selection.End(xlToLeft)).Select
    ActiveSheet.Range(FilterRange).AutoFilter Field:=9, Criteria1:= _
        xlFilterToday, Operator:=xlFilterDynamic, SubField:=10
    ActiveSheet.Range(FilterRange).AutoFilter Field:=9
    Range("B10").Select

Image 1: Before Filter
1586185344158.png


Image 2: After Filter (Result expecting)
1586185443937.png
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I suggest that you never address a question to a specific person...
Could you explain what do you wish to obtain (not "how you tried to obtain it)?

Bye
 
Upvote 0
@Anthony47
I have a Pivot table, where i need to Select Blank Column next to last row of Pivot table and Put Filter. In short need to apply filter in data columns of pivot. (Please see screen shots which i provided )
Steps:
1. select Row from a Blank Column (Here it is "X") next to Last row of Pivot Table till "Column A" (As per Screen shot K to A)
2. Right Click "Blank Column (Here it is "X") next to Last Row of Pivot table
3. Choose "Filter" then "Filter by Selected Cell's Icon"
4. "Select All" in Filter
 
Upvote 0
Are you trying to apply filters to the pivot columns?
Did you try manually if you can do that?

Bye
 
Upvote 0
@Anthony47 Yes It works perfectly when i do it manually. (In screen shot you can see Before Filter and After Filter for your referrence.)

Example:
1. After inserting the pivot, If last pivot column was K, then i will select the J column (blank column) and select till Column A. [ie., Select from J to A column.
2. After Selecting J to A, right click the J column (Blank column) and Choose "Filter" then select "Filter by Selected Cell's Icon"
4. "Select All" in Filter.
 
Upvote 0
I'm sorry I am not able to go any step ahead without a sample pivot table and the description of what you wish to obtain (you explained how you are trying to obtain that)
Bye
 
Upvote 0
@Anthony47 I have uploaded the Sample file for your reference along with what i wish to obtain.., I have colored the line where i need to have a filter (In simple i need filter in that specific line).
Hope this helps for you to figure it out. Please help me.., I am struck with this..,

Steps on how i derived it:
1. After inserting the pivot, If last pivot column was H, then i will select the I column (blank column) and select till Column A. [ie., Select from I to A column.
2. After Selecting I to A, right click the I column (Blank column) and Choose "Filter" then select "Filter by Selected Cell's Icon"
4. "Select All" in Filter.

Sample_File

1586326101295.png
 
Upvote 0
I am still convinced that you cannot apply autofilter to a pivotable data column

The only way I succeded is using this code, that theoretically should insert an autofilter to the empty cell at the right of the data area:
Code:
Sub Pvt_HeadFilt(ByRef PVT As PivotTable)
Dim pSheet As Worksheet, PV1 As String, myMatch
Dim pCC As Long, pRC As Long, FilterCel
Dim TargeTab As String
'
Set pSheet = PVT.Parent
PV1 = PVT.Name
pCC = pSheet.PivotTables(PV1).DataBodyRange.Columns.Count
pRC = pSheet.PivotTables(PV1).DataBodyRange.Rows.Count
FilterCel = pSheet.PivotTables(PV1).DataBodyRange.Cells(1, 1).Offset(-1, pCC).Address
pSheet.AutoFilterMode = False
pSheet.Range(FilterCel).Offset(0, 0).Resize(pRC + 1 - pRC, 1).AutoFilter
End Sub

It is in the form of a Subroutine (a callable Sub) because I recall it from the Sub Worksheet_PivotTableUpdate that I inserted in Sheet2's vba module:
Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
If Target.Name = "PVTable2" Then Call Pvt_HeadFilt(Target)
End Sub

The problem with this erratic setting is that also the Row fields (columns A-B-C in the demo tables) get their autofilter replaced, so they loose their native meaning. To restore their native purpose you have to remove the filters (using Menu /Data /Filter)

This is demonstrated in Sheet2 of the demo workbook; update the Pivottable and the column headers will have a dropdown attached


A second option is to add a column at the right of the pivotable and assign to this column the possibility of filtering based on a selectable field.

This is done with a subroutine like this:
Code:
Sub Pvt_Tot_SideFilt(ByRef PVTT As PivotTable)
'Demo in Sheet3
'
Dim pSheet As Worksheet, cRan As String, myMatch
Dim pCC As Long, pRC As Long, aTots, bTots
Dim pvSh As String, pvCell As String, PV1 As String
Dim FormulaRow As Long, FormulaCol As Long, FilterCel As String
Dim TargeTab As String
'
'
Set pSheet = PVTT.Parent
PV1 = PVTT.Name
pCC = pSheet.PivotTables(PV1).DataBodyRange.Columns.Count
pRC = pSheet.PivotTables(PV1).DataBodyRange.Rows.Count
FilterCel = pSheet.PivotTables(PV1).DataBodyRange.Cells(1, 1).Offset(-1, pCC).Address
'
TargeTab = pSheet.Range(pSheet.PivotTables(PV1).DataBodyRange.Cells(1, 1), _
   pSheet.PivotTables(PV1).DataBodyRange.Cells(1, 1).Offset(0, pCC - 0).Resize(pRC)).Address
pSheet.AutoFilterMode = False
Do While Application.WorksheetFunction.CountIf(Range(FilterCel).Resize(1, 100), "Filter") > 0
    myMatch = Application.Match("Filter", Range(FilterCel).Resize(1, 100), False)
    If IsError(myMatch) Then
        Exit Do
    Else
        Range(Range(FilterCel).Offset(0, myMatch - 2), Range(FilterCel).Offset(0, myMatch - 1).End(xlDown)).ClearContents
    End If
'Stop
Loop
Range(FilterCel).Resize(pRC + 2, 100).Clear
Range(FilterCel).Offset(1, 1).Resize(pRC, 1).FormulaArray = "=Index(" & TargeTab & ",0,Match(" & FilterCel & "," & Range(TargeTab).Offset(-1).Resize(1).Address & ", 0))"
Range(FilterCel) = pSheet.PivotTables(PV1).DataBodyRange.Cells(1, 1).Offset(-1, 0)
Range(FilterCel).Offset(0, 1) = "Filter"
'
With Range(FilterCel).Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=" & pSheet.PivotTables(PV1).DataBodyRange.Offset(-1).Resize(1).Address
    .InCellDropdown = True
End With
ActiveSheet.Range(FilterCel).Offset(0, 1).Resize(pRC + 1, 1).AutoFilter
'Stop
End Sub

It has to be recalled passing as argument the pivottable that have to be managed, and will set at the right of the pivot table a cell that acts as "Index" to the pivot table data field and a column where the content of the selected data field is copied. This added column can be used as filter

I recalled it from the Worksheet_PivotTableUpdate event macro in Sheet3 vba module:
VBA Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Call Pvt_Tot_SideFilt(Target)
End Sub

The problem with this method is that the columns at the right of the pivot table are filled with some information; therefore if you update the table and new columns are necessary to represent all the data fields a message will popup asking if these columns may be overwritten
I don't know of a method for avoiding this popup, a relief could be setting the filter column maybe 5-10 columns to the right of the pivottable, so there is some room for pivot to expand before getting in conflict with the filter column.

The demo file that include what I have been presenting can be downloaded here: byASWINRAJ-MR_Copia di Sample_V-Anth_C00407.xlsm

Bye
 
Upvote 0
I am still convinced that you cannot apply autofilter to a pivotable data column

The only way I succeded is using this code, that theoretically should insert an autofilter to the empty cell at the right of the data area:
Code:
Sub Pvt_HeadFilt(ByRef PVT As PivotTable)
Dim pSheet As Worksheet, PV1 As String, myMatch
Dim pCC As Long, pRC As Long, FilterCel
Dim TargeTab As String
'
Set pSheet = PVT.Parent
PV1 = PVT.Name
pCC = pSheet.PivotTables(PV1).DataBodyRange.Columns.Count
pRC = pSheet.PivotTables(PV1).DataBodyRange.Rows.Count
FilterCel = pSheet.PivotTables(PV1).DataBodyRange.Cells(1, 1).Offset(-1, pCC).Address
pSheet.AutoFilterMode = False
pSheet.Range(FilterCel).Offset(0, 0).Resize(pRC + 1 - pRC, 1).AutoFilter
End Sub

It is in the form of a Subroutine (a callable Sub) because I recall it from the Sub Worksheet_PivotTableUpdate that I inserted in Sheet2's vba module:
Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
If Target.Name = "PVTable2" Then Call Pvt_HeadFilt(Target)
End Sub

The problem with this erratic setting is that also the Row fields (columns A-B-C in the demo tables) get their autofilter replaced, so they loose their native meaning. To restore their native purpose you have to remove the filters (using Menu /Data /Filter)

This is demonstrated in Sheet2 of the demo workbook; update the Pivottable and the column headers will have a dropdown attached


A second option is to add a column at the right of the pivotable and assign to this column the possibility of filtering based on a selectable field.

This is done with a subroutine like this:
Code:
Sub Pvt_Tot_SideFilt(ByRef PVTT As PivotTable)
'Demo in Sheet3
'
Dim pSheet As Worksheet, cRan As String, myMatch
Dim pCC As Long, pRC As Long, aTots, bTots
Dim pvSh As String, pvCell As String, PV1 As String
Dim FormulaRow As Long, FormulaCol As Long, FilterCel As String
Dim TargeTab As String
'
'
Set pSheet = PVTT.Parent
PV1 = PVTT.Name
pCC = pSheet.PivotTables(PV1).DataBodyRange.Columns.Count
pRC = pSheet.PivotTables(PV1).DataBodyRange.Rows.Count
FilterCel = pSheet.PivotTables(PV1).DataBodyRange.Cells(1, 1).Offset(-1, pCC).Address
'
TargeTab = pSheet.Range(pSheet.PivotTables(PV1).DataBodyRange.Cells(1, 1), _
   pSheet.PivotTables(PV1).DataBodyRange.Cells(1, 1).Offset(0, pCC - 0).Resize(pRC)).Address
pSheet.AutoFilterMode = False
Do While Application.WorksheetFunction.CountIf(Range(FilterCel).Resize(1, 100), "Filter") > 0
    myMatch = Application.Match("Filter", Range(FilterCel).Resize(1, 100), False)
    If IsError(myMatch) Then
        Exit Do
    Else
        Range(Range(FilterCel).Offset(0, myMatch - 2), Range(FilterCel).Offset(0, myMatch - 1).End(xlDown)).ClearContents
    End If
'Stop
Loop
Range(FilterCel).Resize(pRC + 2, 100).Clear
Range(FilterCel).Offset(1, 1).Resize(pRC, 1).FormulaArray = "=Index(" & TargeTab & ",0,Match(" & FilterCel & "," & Range(TargeTab).Offset(-1).Resize(1).Address & ", 0))"
Range(FilterCel) = pSheet.PivotTables(PV1).DataBodyRange.Cells(1, 1).Offset(-1, 0)
Range(FilterCel).Offset(0, 1) = "Filter"
'
With Range(FilterCel).Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=" & pSheet.PivotTables(PV1).DataBodyRange.Offset(-1).Resize(1).Address
    .InCellDropdown = True
End With
ActiveSheet.Range(FilterCel).Offset(0, 1).Resize(pRC + 1, 1).AutoFilter
'Stop
End Sub

It has to be recalled passing as argument the pivottable that have to be managed, and will set at the right of the pivot table a cell that acts as "Index" to the pivot table data field and a column where the content of the selected data field is copied. This added column can be used as filter

I recalled it from the Worksheet_PivotTableUpdate event macro in Sheet3 vba module:
VBA Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Call Pvt_Tot_SideFilt(Target)
End Sub

The problem with this method is that the columns at the right of the pivot table are filled with some information; therefore if you update the table and new columns are necessary to represent all the data fields a message will popup asking if these columns may be overwritten
I don't know of a method for avoiding this popup, a relief could be setting the filter column maybe 5-10 columns to the right of the pivottable, so there is some room for pivot to expand before getting in conflict with the filter column.

The demo file that include what I have been presenting can be downloaded here: byASWINRAJ-MR_Copia di Sample_V-Anth_C00407.xlsm

Bye

Thanks for your effort. It really helps a lot..,
 
Upvote 0

Forum statistics

Threads
1,214,872
Messages
6,122,026
Members
449,061
Latest member
TheRealJoaquin

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