Manipulating Pivot Table via VBA

studious

New Member
Joined
Aug 23, 2015
Messages
11
I have daily data that looks like this:

DateCodeFilter 1Filter 2Filter 3Var1Var2Var3
5-Jan-143050YesNoYes15.915.9
5-Jan-143050NoNoYes15.915.9
5-Jan-145470NoYesYes24.7521.121.1
6-Jan-143050YesNoYes15.915.9
6-Jan-143050NoNoYes15.915.9
6-Jan-145470NoYesYes24.7521.121.1
7-Jan-143050YesNoYes15.915.9
7-Jan-143050NoNoYes24.7521.121.1
7-Jan-145470NoYesYes28.5524.524.5

<colgroup><col span="2"><col span="3"><col span="3"></colgroup><tbody>
</tbody>

I have used a pivot table to roll it up to summarize it by day (average) and then grouped it to get to a weekly level. Resulting in this:

Row LabelsAverage of Var1Average of Var2Average of Var3
1/5/2014 - 1/8/201425.718.5888888918.58888889

<colgroup><col><col span="3"></colgroup><tbody>
</tbody>

I am using Filters 1, 2, 3 and Code as filters to in the pivot table to calculate:
  • Average of Var1 for the Week, but only where Code is 3050
  • Percentage difference in Var1 for Code 3050 and Code 5470, calculated as (WeekAvgVar1-3050 - WeekAvgVar1-5470) / WeekAvgVar1-3050&5470
  • The same as above, but with Filter 1 applied for the nominator and Filter 2 applied for the denominator

Since I have to do more of these calculations with different filters on, my question is whether there is a clever way to code/retrieve the results (VBA, I assume) instead of me doing the calculations manually for each case? My concern is that switching filters on and off manually and copy/pasting results is very error-prone and I would really like to avoid it.

Thank you!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Before PivotTables were the SumProduct formulas. Now, depending on version, we have SUMIFS, AVERAGEIFS, AGGREGATE and more.
There is also a strange technique of building a Pivot Table from a Pivot Table.
...and while you may not have access to the add-in for PowerPivot, there is PowerBI desktop with its DAX calculations that can give a once is done.

A VBA solution should be in your reach since it will mostly be a formula and fill-down to have the desired calculations. Since you will establish the "last row" to properly due the fill down you will also have the ranges for the overall averages.

On the Filter question, there are Pivot Table filters that can be applied or Auto-Filters applied to a Pivot Table range...?
 
Upvote 0
Thanks, SpillerBD.

I don't understand the difference in filters you mention. I have the 3 filter variables and Code in the Filters field of the Pivot Table.

You say the calculations with different filters on and off can be done in VBA. I am still novice in VBA, so if you or anyone here could provide a sample for one of the variables to point me in the right direction, I can pick it up from there.

Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,637
Messages
6,125,963
Members
449,276
Latest member
surendra75

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