While the percentage difference works for current month vs previous monthly, and vs previous year without issues for the 40 +sheets within the workbook, i experience runtime error 91 when using current month vs closest quarterly number calculation. Attached is the screenshot for reference. I'm trying to speed up the code and make sure it runs without any hiccups.
VBA Code:
Sub VarCalc()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.StatusBar = False
For i = 1 To Sheets.Count
pos = Sheets(i).Index
Sheets(pos).Activate
With ActiveSheet
If Len(Sheets(i).Name) < 5 Then
Cells.Find(What:="year", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Offset(2).Select
ActiveCell.FormulaR1C1 = "=IFERROR((RC[-2]/RC[-12])-1,0)"
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.End(xlDown))
ActiveCell.Offset(5).Select
ActiveCell.FormulaR1C1 = "=IFERROR((RC[-2]/RC[-12])-1,0)"
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.End(xlDown))
Cells.Find(What:="month", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Offset(2).Select
ActiveCell.FormulaR1C1 = "=IFERROR((RC[-3]/RC[-4])-1,0)"
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.End(xlDown))
ActiveCell.Offset(5).Select
ActiveCell.FormulaR1C1 = "=IFERROR((RC[-3]/RC[-4])-1,0)"
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.End(xlDown))
dcolvar = Cells(1, Columns.Count).End(xlToLeft).Column
If dcolvar Like "2" Or dcolvar Like "4" Or dcolvar Like "7" Or dcolvar Like "10" Then
Cells.Find(What:="qtr", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Offset(2).Select
ActiveCell.FormulaR1C1 = "=IFERROR((RC[-4]/RC[-5])-1,0)"
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.End(xlDown))
ActiveCell.Offset(5).Select
ActiveCell.FormulaR1C1 = "=IFERROR((RC[-4]/RC[-5])-1,0)"
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.End(xlDown))
Else
If dcolvar Like "3" Or dcolvar Like "5" Or dcolvar Like "8" Or dcolvar Like "11" Then
Cells.Find(What:="qtr", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Offset(2).Select
ActiveCell.FormulaR1C1 = "=IFERROR((RC[-4]/RC[-6])-1,0)"
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.End(xlDown))
ActiveCell.Offset(5).Select
ActiveCell.FormulaR1C1 = "=IFERROR((RC[-4]/RC[-6])-1,0)"
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.End(xlDown))
Else
Cells.Find(What:="qtr", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.Offset(2).Select
ActiveCell.FormulaR1C1 = "=IFERROR((RC[-4]/RC[-7])-1,0)"
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.End(xlDown))
ActiveCell.Offset(5).Select
ActiveCell.FormulaR1C1 = "=IFERROR((RC[-4]/RC[-7])-1,0)"
Selection.AutoFill Destination:=Range(ActiveCell, ActiveCell.End(xlDown))
End If
End If
End If
End With
Next i
Exit Sub
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True
Application.StatusBar = True
End Sub