VBA Help

nikhil0311

Board Regular
Joined
May 3, 2013
Messages
200
Office Version
  1. 2013
Platform
  1. Windows

Book1
ABCDEFGH
1Name1-Dec8-Dec15-Dec20-DecAverageCurrent week - Avg%
2Cognizant$5$4$4$9$4$5108%
3Credit Suisse$6$7$8$10$7$343%
4Dominos$3$6$7$8$5$350%
Sheet1



Column A have Name
Date starts from Column B1 and is dynamic i.e. depending the no. of dates,
the range will differ from B1 to X
I want to calculate Average i.e. "=Average(B2:E2").
Average will always exclude latest week. i.e. current range of date is B to E
so we are excluding E. if the range is B to H we will exclude H.


I want to calculate current week minus average i.e. ("=e2-f2"). this should also change as per date range
I want to calculate % i.e. ("=iferror(e2/f2-1,"-")


bottom line if the date column increases, the formulas should auto adjust

below is my current code

Sub Macro1()
'
' Macro1 Macro
'


'
Range("F1").Select
ActiveCell.FormulaR1C1 = "Average"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Current week - Avg"
Range("H1").Select
ActiveCell.FormulaR1C1 = "%"
Range("F2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-4]:RC[-2])"
Selection.AutoFill Destination:=Range("F2:F4")
Range("G2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]-RC[-1]"
Selection.AutoFill Destination:=Range("G2:G4")
Range("H2").Select
ActiveCell.FormulaR1C1 = "=IFERROR(RC[-3]/RC[-2]-1,""-"")"
Selection.AutoFill Destination:=Range("H2:H4")
Range("H2").Select
ActiveCell.FormulaR1C1 = "=IFERROR(RC[-3]/RC[-2]-1,""-"")"
Range("H3").Select
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
How about
Code:
Sub Macro1()
Dim e As Long
e = Range("A1").End(xlToRight).Column
Cells(1, e).Offset(, 1).Resize(, 3).Value = Array("Average", "Current week - Avg", "%")
With Range(Cells(2, e), Cells(4, e))
   .Offset(, 1).FormulaR1C1 = "=AVERAGE(RC2:RC" & e - 1 & ")"
   .Offset(, 2).FormulaR1C1 = "=RC" & e & "-RC" & e + 1 & ""
   .Offset(, 3).FormulaR1C1 = "=IFERROR(RC" & e & "/RC" & e + 1 & "-1,""-"")"
End With
End Sub
 
Upvote 0
This is perfect. Thank you sir, really appreciate it. 1 small change, can we have the % column in this format "0%"?
 
Last edited:
Upvote 0
add this as the last line
Code:
Columns(e + 3).Style = "Percent"
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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