Hello,
I'm trying to create a macro that will filter two different pivot tables in one sheet based on a value in cell A1 of the same sheet. I have some code earlier in this macro that's working fine, so I don't show it here. Below is what I've written to filter the first pivot table in the sheet. I was planning to add the second when I work out the first, which I haven't yet.
Some info - the field I'm filtering for is called "User ID" and I'm filtering for a the value (the user's id) in A1. My Pivot Tables may not always have the same names, so I can't reference the tables by name. I believe you can use the phrase "PivotTables(1)" and "PivotTables(2)" to reference the two tables in the sheet. I tried this is in code earlier and it worked, so I am trying to use that again here. I'm getting an Object Code error on my PTF=... line.
Dim PT As PivotTable
Dim PF As PivotField
Dim PI As PivotItem
Dim PTF As String
'Filter the two pivots for the Username
Set PT = ActiveSheet.PivotTables(1)
Set PF = PT.PivotFields("User ID")
PTF = ActiveSheet.Range("A1").Value
With PT
PF.ClearAllFilters
PF.CurrentPage = PTF
PT.RefreshTable
End With
Thanks for any help/suggestions you can offer - even if it's an entirely different approach than I'm taking here!
Kate
I'm trying to create a macro that will filter two different pivot tables in one sheet based on a value in cell A1 of the same sheet. I have some code earlier in this macro that's working fine, so I don't show it here. Below is what I've written to filter the first pivot table in the sheet. I was planning to add the second when I work out the first, which I haven't yet.
Some info - the field I'm filtering for is called "User ID" and I'm filtering for a the value (the user's id) in A1. My Pivot Tables may not always have the same names, so I can't reference the tables by name. I believe you can use the phrase "PivotTables(1)" and "PivotTables(2)" to reference the two tables in the sheet. I tried this is in code earlier and it worked, so I am trying to use that again here. I'm getting an Object Code error on my PTF=... line.
Dim PT As PivotTable
Dim PF As PivotField
Dim PI As PivotItem
Dim PTF As String
'Filter the two pivots for the Username
Set PT = ActiveSheet.PivotTables(1)
Set PF = PT.PivotFields("User ID")
PTF = ActiveSheet.Range("A1").Value
With PT
PF.ClearAllFilters
PF.CurrentPage = PTF
PT.RefreshTable
End With
Thanks for any help/suggestions you can offer - even if it's an entirely different approach than I'm taking here!
Kate