Dynamically building Pivot Table with VBA

FergalK

New Member
Joined
Jul 24, 2014
Messages
6
Hi
I am currently building an Excel VBA reporting project .
One requirement is for there to be a Pivot Table, which can be built dynamically/change it's fields depending on the source data.
It's all going well so far, but I have a problem as follows:

My Pivot table variables are as follows:
  • PivotDataFieldName
  • PivotDataFieldCaption (value is the same as the Filed name - ie; unchanged)
  • PivotDataFieldAggregation (eg; value is xlCount)

This code works fine:
ActiveSheet.PivotTables("ReportAnalytics_1").adddatafield ActiveSheet.PivotTables("ReportAnalytics_1").PivotFields(PivotDataFieldName), "Count of Stocking ID", xlCount

This code does not work:
ActiveSheet.PivotTables("ReportAnalytics_1").adddatafield ActiveSheet.PivotTables("ReportAnalytics_1").PivotFields(PivotDataFieldName), PivotDataFieldCaption, xlCount

This code does not work:
ActiveSheet.PivotTables("ReportAnalytics_1").adddatafield ActiveSheet.PivotTables("ReportAnalytics_1").PivotFields(PivotDataFieldName), "Count of Stocking ID",PivotDataFieldAggregation

Any idea why it is happy enough using a variable for the Field name, but not for either the Caption or the Aggregation type?

Thanks a lot!

Fergal
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
When you add a data field it becomes another item. It cannot have the same name as the original field. That is why when you add something it has a name "Sum of xxx" or "Count of XXX" because xxx is already taken.

In the data, I tend to make fields that I know will be data fields abbreviations so that I can use the correct name on the data field. So in the data I will have something like "tVal" as the column header so I can use "Value" in the pivot for "Sum of tVal"
 
Upvote 0
When you add a data field it becomes another item. It cannot have the same name as the original field. That is why when you add something it has a name "Sum of xxx" or "Count of XXX" because xxx is already taken.

cool - thanks a lot for that pointer, so now the following line works fine:
ActiveSheet.PivotTables("ReportAnalytics_1").adddatafield ActiveSheet.PivotTables("ReportAnalytics_1").PivotFields(PivotDataFieldName), "Count of" & PivotDataFieldCaption, xlCount

but still having an issue when dynamically assigning the Aggregation type (eg; xlCount):

ActiveSheet.PivotTables("ReportAnalytics_1").adddatafield ActiveSheet.PivotTables("ReportAnalytics_1").PivotFields(PivotDataFieldName), "Count of" & PivotDataFieldCaption, PivotDataFieldAggregation

any ideas on that bit?
Thanks again - this really helps
 
Upvote 0
I have never tried something like that.

What type is PivotDataFieldAggregation? I have never tried to set the dataField function that way. I generally know what it should be. I think if I were taking the value from input I would use a CASE statement to add the field with the right function.

What version of Excel are you using?
 
Upvote 0
Using Excel 2010
The type of PivotDataFieldAggregation is String, I also tried Variant but neither worked
 
Upvote 0
try integer. value of it should be -4112

Look at XLXonsolidationFunction in the developer help.
 
Upvote 0
Wow! That worked!
Excellent :-)
Thanks a million - I think I never would have discovered that solution - that's extremely useful
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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