Option Explicit
Private Sub Workbook_Open()
'This routine prepares the work sheet for data entry
Const cTemplateDelimiter = "{*"
Const cFormulaDelimiter = "="
Const cMinCols = 4
Const cMaxCols = 62
Const cFormulaRow = "19"
Const cMaxRows = 20000
Dim r As Integer
Dim c As Integer
Dim s As Integer
Dim d As Integer
Dim temp As Variant
Dim semp As Variant
Dim bPrep As Boolean
'Assume preparation is not needed
bPrep = False
'Check to see if preparation is required
s = 1
Do
For d = 1 To cMinCols
If InStr(1, Cells(s, d), cTemplateDelimiter, vbTextCompare) > 1 Then
'This is the template so do not run preparation routine
Exit Do
End If
If Cells(s, 1) = "start" Then
bPrep = True
Exit Do
End If
Next
s = s + 1
Loop Until s > cMaxRows
r = 1
Do
For c = 1 To cMinCols
If InStr(1, Cells(r, c), cTemplateDelimiter, vbTextCompare) > 1 Then
'This is the template so do not run preparation routine
Exit Do
End If
If Cells(r, 1) = "prep_required" Then
bPrep = True
Exit Do
End If
If Cells(r, 1) = "subtotal" Then
bPrep = True
Exit Do
End If
Next
r = r + 1
Loop Until r > cMaxRows
'This routine (if required) prepares the sheet for editing
If bPrep Then
s = 1
Do
For d = 1 To cMaxCols
semp = Cells(s, d)
If Left(semp, 1) = cFormulaDelimiter Then
'This cell contains a formula that may need to be converted
semp = Replace(semp, cFormulaRow, Trim(Str(s)), , , vbTextCompare)
End If
Cells(s, d) = semp
Next
If Cells(s, 1) = "start" Then
Cells(s, 1) = ""
Exit Do
End If
s = s + 1
Loop Until s > cMaxRows
r = 1
Do
For c = 1 To cMaxCols
temp = Cells(r, c)
If Left(temp, 1) = cFormulaDelimiter Then
'This cell contains a formula that may need to be converted
If InStr(1, temp, cFormulaRow, vbTextCompare) > 1 Then
'Replace the formula row with the current row
temp = Replace(temp, cFormulaRow, Trim(Str(r)), , , vbTextCompare)
End If
End If
Cells(r, c) = temp
Next
If Cells(r, 1) = "subtotal" Then
Cells(r, 44) = "=SUM(AR" & Trim(Str(s)) & ":AR" & Trim(Str(r - 1)) & ")"
Cells(r, 46) = "=SUM(AT13:AT" & Trim(Str(r - 1)) & ")"
Cells(r, 48) = "=SUM(AV13:AV" & Trim(Str(r - 1)) & ")"
Cells(r, 52) = "=SUM(AZ13:AZ" & Trim(Str(r - 1)) & ")"
Cells(r, 54) = "=SUM(BB13:BB" & Trim(Str(r - 1)) & ")"
Cells(r, 56) = "=SUM(BD13:BD" & Trim(Str(r - 1)) & ")"
Cells(r, 60) = "=SUM(BH13:BH" & Trim(Str(r - 1)) & ")"
Cells(r, 62) = "=SUM(BJ13:BJ" & Trim(Str(r - 1)) & ")"
Cells(r, 1) = ""
End If
If Cells(r, 1) = "prep_required" Then
'Set the Earned Revenue formula
Cells(5, 22) = "=(Z" & Trim(Str(r)) & "-" & "F" & Trim(Str(r)) & "-" & "H" & Trim(Str(r)) & ")/" & "BD" & Trim(Str(r))
'Set the Variance formulas
Cells(2, 46) = "=SUM(AR13:AR" & Trim(Str(r - 1)) & ")"
Cells(3, 46) = "=SUM(AT13:AT" & Trim(Str(r - 1)) & ")"
'Set the Forecast to Complete formulas
Cells(2, 50) = "=SUM(AW13:AW" & Trim(Str(r - 1)) & ")"
Cells(4, 50) = "=SUM(AY13:AY" & Trim(Str(r - 1)) & ")"
Cells(5, 50) = "=SUM(AZ13:AZ" & Trim(Str(r - 1)) & ")"
'Set the New Forecast at Completion formulas
Cells(2, 56) = "=SUM(BD13:BD" & Trim(Str(r - 1)) & ")"
Cells(4, 56) = "=BD3/L4"
Cells(6, 56) = "=Z" & Trim(Str(r)) & "/" & "BD" & Trim(Str(r))
'Set the Change in Forecast formulas
Cells(2, 62) = "=SUM(BH13:BH" & Trim(Str(r - 1)) & ")"
Cells(3, 62) = "=SUM(BJ13:BJ" & Trim(Str(r - 1)) & ")"
'Set the Total formulas
Cells(r, 32) = "=SUM(AF13:AF" & Trim(Str(r - 1)) & ")/2 "
Cells(r, 36) = "=SUM(AJ13:AJ" & Trim(Str(r - 1)) & ")/2"
Cells(r, 38) = "=SUM(AL13:AL" & Trim(Str(r - 1)) & ")/2"
Cells(r, 42) = "=SUM(AP13:AP" & Trim(Str(r - 1)) & ")/2"
Cells(r, 44) = "=SUM(AR13:AR" & Trim(Str(r - 1)) & ")"
Cells(r, 46) = "=SUM(AT13:AT" & Trim(Str(r - 1)) & ")"
Cells(r, 48) = "=SUM(AV13:AV" & Trim(Str(r - 1)) & ")"
Cells(r, 52) = "=SUM(AZ13:AZ" & Trim(Str(r - 1)) & ")"
Cells(r, 54) = "=SUM(BB13:BB" & Trim(Str(r - 1)) & ")"
Cells(r, 56) = "=SUM(BD13:BD" & Trim(Str(r - 1)) & ")"
Cells(r, 58) = "=Z" & Trim(Str(r)) & "/" & "BD" & Trim(Str(r))
Cells(r, 60) = "=SUM(BH13:BH" & Trim(Str(r - 1)) & ")"
Cells(r, 62) = "=SUM(BJ13:BJ" & Trim(Str(r - 1)) & ")"
Cells(r, 1) = ""
Exit Do
End If
r = r + 1
Loop Until r > cMaxRows
End If
End Sub