happyhungarian
Active Member
- Joined
- Jul 19, 2011
- Messages
- 254
- Office Version
- 365
- Platform
- Windows
Hi, I modified the following code to refresh a set of filters in a pivot table based upon the values that are placed in cell range. I thought this code could work but I'm getting a "Sub or Function not defined" error. Any idea where I could have messed up this code? Do you think this will ultimately work?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, 'it only updates when the cells are touched
If Intersect(Target, Range("D2:D6")) Is Nothing Then Exit Sub
'Set the Variables to be used
Dim pt As PivotTable
Dim FieldOrganization As PivotField
Dim FieldVP As PivotField
Dim FieldOrg_Function As PivotField
Dim FieldCC_Owner As PivotField
Dim FieldCost_Center As PivotField
Dim NewOrganization As String
Dim NewVP As String
Dim NewOrg_Function As String
Dim NewCC_Owner As String
Dim NewCost_Center As String
'Amend here to filter your data
Set pt = Worksheets("Travel").PivotTables("PivotTable1")
Set FieldRegion = pt.PivotFields("Organization")
Set FieldDept = pt.PivotFields("VP")
Set FieldDept = pt.PivotFields("Org Function")
Set FieldDept = pt.PivotFields("CC Owner")
Set FieldDept = pt.PivotFields("Cost Center")
NewOrganization = Worksheets("Trended P&L").Range("D2").Value
NewVP = Worksheets("Trended P&L").Range("D3").Value
NewOrg_Function = Worksheets("Trended P&L").Range("D4").Value
NewCC_Owner = Worksheets("Trended P&L").Range("D5").Value
NewCost_Center = Worksheets("Trended P&L").Range("D6").Value
'This updates and refreshes the PIVOT table
With pt
FieldRegion.ClearAllFilters
FieldRegion.CurrentPage = NewOrganization
FieldDept.ClearAllFilters
FieldDept.CurrentPage = NewVP
FieldDept.ClearAllFilters
FieldDept.CurrentPage = NewOrg_Function
FieldDept.ClearAllFilters
FieldDept.CurrentPage = NewCC_Owner
FieldDept.ClearAllFilters
FieldDept.CurrentPage = NewCost_Center
pt.RefreshTable
EndWith
EndSub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, 'it only updates when the cells are touched
If Intersect(Target, Range("D2:D6")) Is Nothing Then Exit Sub
'Set the Variables to be used
Dim pt As PivotTable
Dim FieldOrganization As PivotField
Dim FieldVP As PivotField
Dim FieldOrg_Function As PivotField
Dim FieldCC_Owner As PivotField
Dim FieldCost_Center As PivotField
Dim NewOrganization As String
Dim NewVP As String
Dim NewOrg_Function As String
Dim NewCC_Owner As String
Dim NewCost_Center As String
'Amend here to filter your data
Set pt = Worksheets("Travel").PivotTables("PivotTable1")
Set FieldRegion = pt.PivotFields("Organization")
Set FieldDept = pt.PivotFields("VP")
Set FieldDept = pt.PivotFields("Org Function")
Set FieldDept = pt.PivotFields("CC Owner")
Set FieldDept = pt.PivotFields("Cost Center")
NewOrganization = Worksheets("Trended P&L").Range("D2").Value
NewVP = Worksheets("Trended P&L").Range("D3").Value
NewOrg_Function = Worksheets("Trended P&L").Range("D4").Value
NewCC_Owner = Worksheets("Trended P&L").Range("D5").Value
NewCost_Center = Worksheets("Trended P&L").Range("D6").Value
'This updates and refreshes the PIVOT table
With pt
FieldRegion.ClearAllFilters
FieldRegion.CurrentPage = NewOrganization
FieldDept.ClearAllFilters
FieldDept.CurrentPage = NewVP
FieldDept.ClearAllFilters
FieldDept.CurrentPage = NewOrg_Function
FieldDept.ClearAllFilters
FieldDept.CurrentPage = NewCC_Owner
FieldDept.ClearAllFilters
FieldDept.CurrentPage = NewCost_Center
pt.RefreshTable
EndWith
EndSub