VBA PivotField referencing drop down cell

Lindsay Flink 1 Reputation point
2021-03-17T15:00:35.657+00:00

I am trying to write VBA to adjust the row selection on a PivotTable. This is what I have so far

Sub Addings_PivotFields()  
Dim pt As PivotTable  
Dim pf As PivotField  
    
    Set pt = ActiveSheet.PivotTables("PivotTable2")  
        For Each pf In pt.RowFields  
        pf.Orientation = xlHidden  
    Next pf  
  
    Set pt = ActiveSheet.PivotTables("PivotTable2")  
        pt.PivotFields("Category").Orientation = xlRowField  
      
End Sub  

This works as is, but I'd like the pt.PivotFields("Category") to reference a cell that contains a list so I can toggle between it being grouped by Category and Account.

How do I get the last portion of code to reference cell D2 instead of typing in a column name?

Tables below for reference:
78869-capture.png

78834-capture2.png
78873-capture3.png

0 comments No comments
{count} votes