• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to control Excel PIVOT table filters from cell values with VBA

LarryM

New Member
I'm trying to refresh my pivot table base on two cell values. End user will type in value in cell B4 (Region) and value in cell B5 (Department). Pivot table would refresh base on these values.

I found code below, but only allows for referencing 1 cell value. Not sure how to modify it for 2 cell values.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'This line stops the worksheet updating on every change, 
'it only updates when cell'B4 or B5 is touched

If Intersect(Target, Range("B4:B5")) Is Nothing Then Exit Sub

'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat AsString

'Amend here to filter your data
Set pt = Worksheets("Sheet1").PivotTables("PivotTable1")
Set Field = pt.PivotFields("Region")
NewCat = Worksheets("Sheet1").Range("B4").Value

'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
EndWith

EndSub
 
Hi

Appreciate if you can upload a your sample file.

I could not try this myself as I was not having time to create dummy data, but just thinking if this could solve.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'This line stops the worksheet updating on every change, 'it only updates when cell'B4 or B5 is touched
If Intersect(Target, Range("B4:B5")) Is Nothing Then Exit Sub

'Set the Variables to be usedDim pt As PivotTable
Dim FieldRegion As PivotField
Dim FieldDept As PivotField
Dim NewRegion As String
Dim NewDept As String

'Amend here to filter your dataSet pt = Worksheets("Sheet1").PivotTables("PivotTable1")
Set FieldRegion = pt.PivotFields("Region")
Set FieldDept = pt.PivotFields("Department")
NewRegion = Worksheets("Sheet1").Range("B4").Value
NewDept = Worksheets("Sheet1").Range("B5").value

'This updates and refreshes the PIVOT tableWith pt
FieldRegion.ClearAllFilters
FieldRegion.CurrentPage = NewRegion
FieldDept.ClearAllFilters
FieldDept.CurrentPage = NewDept
pt.RefreshTable
EndWith

EndSub

Please let me know if this works.

Regards,
Prasad DN
 
In the above code, some part of the coding as gone under comment part. kindly make changes as mentioned below:
'Set the Variables to be used
Dim pt As PivotTable

And in
'This updates and refreshes the PIVOT table
With pt

Regret if any inconvenience caused. I could not edit it to correct my post. hence adding this reply.

Regards,
Prasad DN
 
Hi

Appreciate if you can upload a your sample file.

I could not try this myself as I was not having time to create dummy data, but just thinking if this could solve.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'This line stops the worksheet updating on every change, 'it only updates when cell'B4 or B5 is touched
If Intersect(Target, Range("B4:B5")) Is Nothing Then Exit Sub

'Set the Variables to be usedDim pt As PivotTable
Dim FieldRegion As PivotField
Dim FieldDept As PivotField
Dim NewRegion As String
Dim NewDept As String

'Amend here to filter your dataSet pt = Worksheets("Sheet1").PivotTables("PivotTable1")
Set FieldRegion = pt.PivotFields("Region")
Set FieldDept = pt.PivotFields("Department")
NewRegion = Worksheets("Sheet1").Range("B4").Value
NewDept = Worksheets("Sheet1").Range("B5").value

'This updates and refreshes the PIVOT tableWith pt
FieldRegion.ClearAllFilters
FieldRegion.CurrentPage = NewRegion
FieldDept.ClearAllFilters
FieldDept.CurrentPage = NewDept
pt.RefreshTable
EndWith

EndSub

Please let me know if this works.

Regards,
Prasad DN


I've attached my sample file with your code included in it. It does not seem to be running. any help is greatly appreciated.
 

Attachments

  • PIVOT-Table-filter-linked-to-cell-value-end-file.xlsm
    22.9 KB · Views: 318
Last edited:
Hi,

I guess in the code "t" is missing when you typed "Ref by Dept".
Issue line:
Code:
Set FieldDept = pt.PivotFields("Ref by Dep")
'change this to
 
Set FieldDept = pt.PivotFields("Ref by Dept")

Regards,
Prasad DN
PS: its working when I did the above changes. :)
 
Back
Top