I am trying to create an interactive pivot table by allowing the user to select which data field to show (ie dollars earned, dollars spent). I have the following code that works
Sub AddMoreFields()
With ActiveSheet.PivotTables("PivotTable1")
.AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Dollars_spent")
End With
End Sub
However, i want to change it to something like the following, so that a user can select which data to show
Sub AddMoreFields()
With ActiveSheet.PivotTables("PivotTable1")
.AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields(Range("d21"))
End With
End Sub
Range("D21") would actually be a drop down list in which a user could select between dollars earned and dollars spent. However, when i try use this i get an error stating that Rang("D21") is not a pivot field.
Any suggestions?
Sub adddynamic()
Dim pt As PivotTable
Dim pf As PivotField
Dim f As String
f = Range("d21")
Set pt = ActiveSheet.PivotTables("PivotTable1")
Set pf = pt.PivotFields(f)
pt.AddDataField pf, "Sum of " & f, xlSum
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.