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
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