VBA Edit Pivot field based on variable

Vrilx

New Member
Joined
Jul 25, 2019
Messages
7
Hey guys, need a little help and can't seem to find the answer i need.

I have VBA code that creates a pivot table and my first pass hardcoded the Values chosen into the building of the table. I would like to try and simplify my code and use a variable to define what field is put into the Values column of the pivot table;

So first i have done this;

VBA Code:
Dim release As Range

Set release = Range("B9:D9")

The range is merged cells with drop down applied, users can choose "Release1, Release2, Release3 etc" All of the options in this drop down exist in the base data for pivot.

Then in the code itself i am trying to get the pivot table to look at the variable and pull back the correct field into the values, this is where i find my issues;

VBA Code:
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
        "PivotTable2").PivotFields(release), "Sum of" & release, xlSum

any ideas? Thanks in advance as always you bunch of helpful people!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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