I'm working with table which looks like this [link]. Every month increases for one column and I have to manually correct formulas. I'm trying to make things easier so I start creatin a macro. As I've never write any code, I don't know how to make correct loops to fill all cells with appropriate formulas.
I did following code to fill first four coloumns with appropriate formulas:
I also have problem with filling last column with appropriate formula. It's year to date comparison, so it should be like SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15]), increasing by 1 (monthly data) but there need to be loop to allow it only to 12 repeats, then should again start from (RC[-1]/RC[-2])*100.
Could you please help me?
Thank you in advance!
ps: I'm working on Excel 2010.
I did following code to fill first four coloumns with appropriate formulas:
Code:
Sub Insert()
Cells.Find(what:="Sales Units", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Cells.Find(what:="CP/R1", After:=ActiveCell, LookIn:=xlFormulas, lookat _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.EntireColumn.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Offset(1, 1).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(4, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-1]/RC[-2])*100"
Selection.Offset(-20, 1).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(4, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(RC[-2]/RC[-14])*100"
Selection.Offset(-20, 1).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(4, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15])*100"
Selection.Offset(-20, 1).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(4, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
Selection.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "=(SUM(RC[-15]:RC[-4])/SUM(RC[-27]:RC[-16]))*100"
End Sub
I also have problem with filling last column with appropriate formula. It's year to date comparison, so it should be like SUM(RC[-4]:RC[-3])/SUM(RC[-16]:RC[-15]), increasing by 1 (monthly data) but there need to be loop to allow it only to 12 repeats, then should again start from (RC[-1]/RC[-2])*100.
Could you please help me?
Thank you in advance!
ps: I'm working on Excel 2010.