Dynamic Pivot Table Based on Drop-Down List Items

marduino

New Member
Joined
Dec 24, 2021
Messages
10
Office Version
  1. 2010
Platform
  1. Windows
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!
 

Attachments

  • Dropdowns&PivotTable.JPG
    Dropdowns&PivotTable.JPG
    141.1 KB · Views: 28

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
See if the following helps you.
First, if the pivot table doesn't exist, you can create it with the following macro:

VBA Code:
Sub CreatePivotTable()
  ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
    SourceData:="Table1").CreatePivotTable _
    TableDestination:="'WIN%'!R3C5", TableName:="PivotTable1"
End Sub

If the pivot table already exists, then the data in the cells should look like this:
varios 15may2022.xlsm
AB
1PAGE
2INPUT 1JOCKEY
3FILTER 1Luis
4
5ROW
6INPUT 1SURFACE
7FILTER 1
8INPUT 2DISTANCE (F)
9FILTER 2
10
11VALUES
12INPUT1WIN
13INPUT2# of RACES
14INPUT3WIN%
WIN%


Try the following macro to populate the pivot table.
VBA Code:
Sub PivotTable()
  Dim sh1 As Worksheet
  Dim tbl As PivotTable
  Dim input1 As String, input2 As String, input3 As String
  
  Set sh1 = Sheets("WIN%")
  Set tbl = sh1.PivotTables("PivotTable1")
  tbl.ClearTable
  tbl.PivotCache.Refresh
  
  'Page
  With tbl.PivotFields(Range("B2").Value)
    .Orientation = xlPageField
    .Position = 1
    .ClearAllFilters
    .CurrentPage = sh1.Range("B3").Value
  End With
  
  'Rows
  If Range("B6").Value <> "" Then
    With tbl.PivotFields(Range("B6").Value)
      .Orientation = xlRowField
      .Position = 1
    End With
  End If
  If Range("B8").Value <> "" Then
    With tbl.PivotFields(Range("B8").Value)
      .Orientation = xlRowField
      .Position = 2
    End With
  End If
  
  'Values
  input1 = sh1.Range("B12").Value
  If input1 <> "" Then
    tbl.AddDataField tbl.PivotFields(input1), "Sum of " & input1, xlSum
  End If
  input2 = sh1.Range("B13").Value
  If input2 <> "" Then
    tbl.CalculatedFields.Add "# of RACES", "=WIN+LOSS", True
    tbl.AddDataField tbl.PivotFields(input2), "Sum of " & input2, xlSum
  End If
  input3 = sh1.Range("B14").Value
  If input3 <> "" Then
    tbl.CalculatedFields.Add "WIN%", "=WIN/(WIN+LOSS)", True
    tbl.AddDataField tbl.PivotFields(input3), "Sum of " & input3, xlSum
  End If
End Sub


Results:
1652620302153.png


Note: I did not integrate the filters for the rows, but check if you can integrate them.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top