Updating pivot filters from fields

Dalewo

New Member
Joined
Jan 12, 2011
Messages
3
Hi All

I know this question has been asked to death so i have followed a guide here: http://www.mrexcel.com/forum/excel-...ot-table-filter-based-value-another-cell.html

The problem is i want to update a number of filters from fields, to do this i have tried to copy the VBA twice with a few tweaks but still didnt get anywhere. The first Filter 'Fin_YR' works fine, the second does nothing beside refresh the page....

It would be good to include it all in one macro but my knowledge doesnt go that far :( any help is appreciated


Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)'This line stops the worksheet updating on every change, it only updates when cell
'H6 or H7 is touched
If Intersect(Target, Worksheets(1).Range("H1:H2")) Is Nothing Then Exit Sub


'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim pivot_item As PivotItem
Dim NewYear As String
Dim test_val As String


'Here you amend to suit your data
Set pt = Worksheets(1).PivotTables("TestTable1")
Set Field = pt.PivotFields("Fin_YR")
NewYear = Worksheets(1).Range("H1").Value


'Here is the test if the input field exists
test_val = NewYear
For Each pivot_item In pt.PivotFields("Fin_YR").PivotItems
    If pivot_item.Name = test_val Then
        Exit For
    End If
Next pivot_item
On Error Resume Next


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


End Sub


Private Sub Workbook_SheetChange_Acct(ByVal Sh As Object, ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell
'H6 or H7 is touched
If Intersect(Target, Worksheets(1).Range("H1:H3")) Is Nothing Then Exit Sub


'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim pivot_item As PivotItem
Dim AcctMgr As String
Dim test_val_acctMgr As String


'Here you amend to suit your data
Set pt = Worksheets(1).PivotTables("TestTable1")
Set Field = pt.PivotFields("Account Manager")
AcctMgr = Worksheets(1).Range("H2").Value


'Here is the test if the input field exists
test_val_acctMgr = AcctMgr
For Each pivot_item In pt.PivotFields("Account Manager").PivotItems
    If pivot_item.Name = test_val_acctMgr Then
        Exit For
    End If
Next pivot_item
On Error Resume Next


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


End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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