Hello,
I am trying to setup a VBA script to generate a pivot table based on user selections from drop-down (and dependent-drop down) menus of items taken from "Table1". I've done a bit of looking around online and I can't seem to find anyone else having tried to accomplish this, or if this is even possible, so I am turning to the experts here for help!
I've attached an image of the cell layout for the drop-down menus, and an example of one of the pivot table results I am trying to automate. The issue that I am having is that I am not sure how to go about dynamically referencing the the drop-down inputs in the script for the "PivotFields(...)" items. Below is a template for the code, the B2, B3, B6, etc...values are serving as a placeholder for where the input values should go. In theory I think that the PivotField items should reference a column number in the source data (which i can use a MATCH formula to output), but I am still not sure how to properly code that or reference filters that are derived from dependent drop-drop down items.
VBA:
Sub PivotTable()
PivotCaches.Clear
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table1", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination _
:="'WIN%'!R1C5", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion14
Sheets("WIN%").Select
Cells(1, 5).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields(B2)
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields(B6)
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields(B8)
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields(B2)
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields(B2).ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields(B2).CurrentPage = _
B3
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("WIN"), "Sum of WIN", xlSum
ActiveSheet.PivotTables("PivotTable1").CalculatedFields.Add "# of RACES", _
"=WIN+LOSS", True
ActiveSheet.PivotTables("PivotTable1").PivotFields("# of RACES").Orientation = _
xlDataField
ActiveSheet.PivotTables("PivotTable1").CalculatedFields.Add "WIN%", _
"=WIN/(WIN+LOSS)", True
ActiveSheet.PivotTables("PivotTable1").PivotFields("WIN%").Orientation = _
xlDataField
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Sum of WIN%"), "Sum of WIN%", xlSum
End Sub
As always, thank you for your time and efforts!
I am trying to setup a VBA script to generate a pivot table based on user selections from drop-down (and dependent-drop down) menus of items taken from "Table1". I've done a bit of looking around online and I can't seem to find anyone else having tried to accomplish this, or if this is even possible, so I am turning to the experts here for help!
I've attached an image of the cell layout for the drop-down menus, and an example of one of the pivot table results I am trying to automate. The issue that I am having is that I am not sure how to go about dynamically referencing the the drop-down inputs in the script for the "PivotFields(...)" items. Below is a template for the code, the B2, B3, B6, etc...values are serving as a placeholder for where the input values should go. In theory I think that the PivotField items should reference a column number in the source data (which i can use a MATCH formula to output), but I am still not sure how to properly code that or reference filters that are derived from dependent drop-drop down items.
VBA:
Sub PivotTable()
PivotCaches.Clear
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table1", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination _
:="'WIN%'!R1C5", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion14
Sheets("WIN%").Select
Cells(1, 5).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields(B2)
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields(B6)
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields(B8)
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields(B2)
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields(B2).ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields(B2).CurrentPage = _
B3
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("WIN"), "Sum of WIN", xlSum
ActiveSheet.PivotTables("PivotTable1").CalculatedFields.Add "# of RACES", _
"=WIN+LOSS", True
ActiveSheet.PivotTables("PivotTable1").PivotFields("# of RACES").Orientation = _
xlDataField
ActiveSheet.PivotTables("PivotTable1").CalculatedFields.Add "WIN%", _
"=WIN/(WIN+LOSS)", True
ActiveSheet.PivotTables("PivotTable1").PivotFields("WIN%").Orientation = _
xlDataField
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Sum of WIN%"), "Sum of WIN%", xlSum
End Sub
As always, thank you for your time and efforts!