macro for pivot calculated item

vexcelmacro

New Member
Joined
Apr 16, 2011
Messages
4
would like to have calculate Average for the below table through macro.

Region Subregion QTR Revenue 10Q4 11Q1 11Q2
AAA QAZ 10Q1 1,000
AAA WSX 10Q1 2,000
EEE EDC 10Q1 3,000
EEE EDC 10Q2 4,000
HHH WSX 10Q3 5,000
SSS TGB 10Q4 6,000
QQQ YHN 10Q4 7,000
YYY UJM 11Q1 8,000
TTT Nordics 11Q1 9,000
UUU QW 11Q2 10,000
III ER 11Q2 11,000


The QTR column data will get updated on every new quarters for example now we have till 11Q2 after june the QTR column will get updated with new QTR as 11Q3 (which will continue for three months)

I need a macro to add four quarter and divide by 4 and the heading should be the QTR name for example :

when the table is updated with new quarter the calcuation should change its running total as, now we are in 11Q2 so the calculation should be like this

11Q2(heading of the row)=11Q2+11Q1+10Q4+10Q3)/4 when the table is updated with 11Q3 the calculation should change as shown below

11Q3(heading of the row)=11Q3+11Q2++11Q1+10Q4)/4
11Q2(heading of the row)=11Q2+11Q1+10Q4+10Q3)/4

I have a data till 2007(07Q1,07Q2,07Q3,07Q4) every year will have four quarters, and i need my macro to calculate the running total based on the new quarters as the example shown above.

If you have any better suggestion kindly share...



I used the pivot logic (formula calculated item) over here and below is my macro, but unfortunately the avg variable is not working in my macro.

Sub test()
a = Sheets("Pivot").Range("A1").Value
Sheets("List").Select
Cells.Find(what:=a, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
'lastrow = Range("A65000").End(xlUp).Select

a = ActiveCell
b = ActiveCell.Offset(-1)
C = ActiveCell.Offset(-2)
d = ActiveCell.Offset(-3)
e = 4

AVG = "(" & a & "+" & b & "+" & C & "+" & d & ")" & "/" & e
'AVG = """avg11q1""" & "," & "=" & "(" & a & "+" & b & "+" & c & "+" & d & ")"
MsgBox (AVG)


Sheets("Pivot").Select
Range("c4").Select

ActiveSheet.PivotTables("PivotTable1").PivotFields("QTR").CalculatedItems.Add Name:="AVGQTR11", Formula:="=" & AVG


End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Why don't you use real dates instead of text items for the quarters, and then group the date by quarter and year, and set the field setting as average, like this:

Excel Workbook
ABCDEFGH
1RegionSubregionDateRevenueAverage of Revenue
2AAAQAZ01/01/20101,000YearsDateTotal
3AAAWSX01/01/20102,0002010Qtr12000
4EEEEDC01/01/20103,000Qtr24000
5EEEEDC01/04/20104,000Qtr35000
6HHHWSX01/07/20105,000Qtr46500
7SSSTGB01/10/20106,0002011Qtr18500
8QQQYHN01/10/20107,000Qtr210500
9YYYUJM01/01/20118,000Grand Total6000
10TTTNordics01/01/20119,000
11UUUQW01/04/201110,000
12IIIER01/04/201111,000
Sheet3



?
 
Upvote 0
Thanks for the suggestion, but my real data is in the below format and my manager would like to know the average of each quarter based on region and subregion, this works fine in pivot calculateditems but since its a dashboard every quarter manually intervention is good. so i would like to automate this.


source data
Region Subregion QTR Revenue
AAA QAZ 10Q1 1,000
AAA WSX 10Q1 2,000
EEE EDC 10Q1 3,000
EEE EDC 10Q2 4,000
HHH WSX 10Q3 5,000
III ER 11Q2 11,000
QQQ YHN 10Q4 7,000
SSS TGB 10Q4 6,000
TTT Nordics 11Q1 9,000
UUU QW 11Q2 10,000
YYY UJM 11Q1 8,000

Result next to the region and subregion qtr column

11Q3(heading of the row)=11Q3+11Q2++11Q1+10Q4)/4 (next quarter will come)
11Q2(heading of the row)=11Q2+11Q1+10Q4+10Q3)/4
11Q1(heading of the row)=11Q1+10Q4+10Q3+10Q2)/4
10Q4(heading of the row)=10Q4+10Q3+10Q2+10Q1)/4

i have till 2007 data
 
Upvote 0
Look at this example of formulas to do high level summing for rolling 4 quarter SUM ( not average, but the same logic applies ... this is so that you can see what can be done ... you'd simply divide by another SUMPRODUCT that does a count of the same matches ). Addition logic can be applied to the SUMPRODUCT to limit calcs to a particular region and subregion:

Excel Workbook
ABCDEFGHIJ
1RegionSubregionDateRevenue10Q110Q210Q310Q411Q111Q2
2AAAQAZ10Q11,000249332610926110280003900056000
3AAAWSX10Q12,000
4EEEEDC10Q13,00008Q3
5EEEEDC10Q24,00008Q4
6HHHWSX10Q35,00009Q1
7SSSTGB10Q46,00009Q2
8QQQYHN10Q47,00009Q3
9YYYUJM11Q18,00009Q4
10TTTNordics11Q19,00010Q1
11UUUQW11Q210,00010Q2
12IIIER11Q211,00010Q3
13BBBQWS09Q21,50010Q4
14DSDRRF09Q21,32411Q1
15TREQAS09Q31,55511Q2
16GTHKJL09Q33,44411Q3
17HHJLKL09Q44,55511Q4
18JUJOLO09Q46,55512Q1
Qtr calc
 
Upvote 0
Hi
Thanks for the formula, but will this work based on each region and subregion, same what the pivot tool does ?

I need average of quarter based on each region and subregion.
for example:
i use excel 2007, if you create a pivot from the data provided by me, and just pull the region and subregion column to 'Row Labels' of pivot tool and QTR to 'Column Labels' and revenue to 'Values', after that go to pivot formulas calculated item and create new formula manually for the current quarter as shown below
11Q2=(11Q2+11Q1+10Q4+10Q3)/4 the result what ever you get here is my request.
Previous quarter is fine i can do it manually for first time but is it possible to create macro for the new quarter example
11Q3=11Q3+11Q2++11Q1+10Q4)/4
i have a code with me but i was unable to pass variable on the calculated item , i listed all the quarter to a list sheet.
Sub test()
a = Sheets("Pivot").Range("A1").Value
Sheets("List").Select
Cells.Find(what:=a, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
'lastrow = Range("A65000").End(xlUp).Select

a = ActiveCell
b = ActiveCell.Offset(-1)
C = ActiveCell.Offset(-2)
d = ActiveCell.Offset(-3)
e = 4

AVG = "(" & a & "+" & b & "+" & C & "+" & d & ")" & "/" & e
'AVG = """avg11q1""" & "," & "=" & "(" & a & "+" & b & "+" & c & "+" & d & ")"
MsgBox (AVG)


Sheets("Pivot").Select
Range("c4").Select

ActiveSheet.PivotTables("PivotTable1").PivotFields("QTR").CalculatedItems.Add Name:="AVGQTR11", Formula:="=" & AVG


End Sub
 
Upvote 0
Yes, it will work based on a region and subregion that you give it, like this:

Excel Workbook
ABCDEFGHIJKLM
1RegionSubregionDateRevenueRegionSubregion10Q110Q210Q310Q411Q111Q2
2AAANordics10Q11,000AAANordics153231399915555160002100032000
3BBBIberics10Q12,000
4AAANordics10Q13,00008Q3
5BBBIberics10Q24,00008Q4
6AAANordics10Q35,00009Q1
7BBBIberics10Q46,00009Q2
8AAANordics10Q47,00009Q3
9BBBIberics11Q18,00009Q4
10AAANordics11Q19,00010Q1
11BBBIberics11Q210,00010Q2
12AAANordics11Q211,00010Q3
13BBBIberics09Q21,50010Q4
14AAANordics09Q21,32411Q1
15BBBIberics09Q31,55511Q2
16AAANordics09Q33,44411Q3
17BBBIberics09Q44,55511Q4
18AAANordics09Q46,55512Q1
Qtr calc
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,135
Members
452,890
Latest member
Nikhil Ramesh

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