'Remove a variable filter on pivot table

I'm struggling to solve the below issue:

I need to write a code that refresh a pivot table adding a filed and removing another one. I managed to make it work at least for the refresh and addition of new field. The issue is that it gives me error for the part to delete the existing field i called "pf_Name_old" that is a variable one. Below my code

Sub aggiungi_filtriPivot()


ActiveWorkbook.Worksheets("Dashboard").Select

'PURPOSE: Add A Values Field to a Pivot Table and remove old month field


Dim pvt As PivotTable
Dim pf As String
Dim pf_Name As String


mese = Sheets("Console").Cells(6, 3).Value


pf = mese
pf_Name = "Count of " & mese
pf_Name_old = Sheets("Base Dati").Cells(4, 1).Value

Set pvt = ActiveSheet.PivotTables("PivotTable3")
Set pvt1 = ActiveSheet.PivotTables("PivotTable4")
Set pvt2 = ActiveSheet.PivotTables("PivotTable1")


pvt.AddDataField pvt.PivotFields, pf_Name, xlCount
'Issue is here below
ActiveSheet.PivotTables("PivotTable3").PivotFields(pf_Name_old).Orientation = xlHidden 
pvt1.AddDataField pvt.PivotFields, pf_Name, xlCount
pvt2.AddDataField pvt.PivotFields, pf_Name, xlCount

End Sub

Any help on how can i make it work? it seems i cannot use a variable field with the above code



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source