Option Explicit
'https://www.mrexcel.com/forum/excel-questions/1077672-automatically-updating-calendar.html
Sub ChangingCalendar()
'Uses formulas part of the calendar by John Walkenbach
'http://spreadsheetpage.com/index.php/file/yearly_calendar_with_holidays/
'Change the Const line to refer to the worksheet in this workbook that
' holds the date of interest in cell B2
Const sDataWksName As String = "Sheet1"
'Next Const line holds the name of the sheet in this workbook that
' will be created to hold the calendar
Const sWorksheet As String = "Two Month Calendar"
'Next Const line contains the text value of the range of the upper left cell of the calendar
Const sULCell As String = "B2"
Dim theFormulaPart1 As String
Dim theFormulaPart2 As String
'Delete & Create the Calendar Page
On Error Resume Next
Application.DisplayAlerts = False
Worksheets(sWorksheet).Delete
Application.DisplayAlerts = True
On Error GoTo 0
Worksheets.Add(After:=Sheets(Sheets.Count)).Name = sWorksheet 'After last
With Worksheets(sWorksheet)
'Basic Format Calendar Page
Range(.Range(sULCell), .Range(sULCell).Offset(0, 6)).MergeCells = True
Range(.Range(sULCell).Offset(0, 2), .Range(sULCell).Offset(0, 8)).MergeCells = True
.Range(sULCell).NumberFormat = "mmmm yyyy"
.Range(sULCell).Offset(0, 2).NumberFormat = "mmmm yyyy"
.Range("B3:H3,J3:P3").NumberFormat = "ddd"
.Range("B4:H9,J4:P9").NumberFormat = "0"
With .Range("B2:P9")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
'Add Formulas to Calendar Page
.Range("B2").FormulaR1C1 = _
"=DATE(YEAR('" & sDataWksName & "'!R2C2),MONTH(" & sDataWksName & "!R2C2),1)"
.Range("J2").FormulaR1C1 = _
"=DATE(YEAR('" & sDataWksName & "'!R2C2),MONTH(" & sDataWksName & "!R2C2)+1,1)"
.Range("B3:H3").FormulaArray = "=TRANSPOSE({1;2;3;4;5;6;7})"
.Range("J3:P3").FormulaArray = "=TRANSPOSE({1;2;3;4;5;6;7})"
'The next line is too long to enter using VBA
'Range("A3:G8").FormulaR1C1 = _
"=IF(MONTH(DATE(YEAR(R1C),MONTH(R1C),1))<>MONTH(DATE(YEAR(R1C),MONTH(R1C),1)-(WEEKDAY(DATE(YEAR(R1C),MONTH(R1C),1))-1)+{0;1;2;3;4;5}*7+{1,2,3,4,5,6,7}-1),"""",DATE(YEAR(R1C),MONTH(R1C),1)-(WEEKDAY(DATE(YEAR(R1C),MONTH(R1C),1))-1)+{0;1;2;3;4;5}*7+{1,2,3,4,5,6,7}-1)"
'Solution found at:
'http://dailydoseofexcel.com/archives/2005/01/10/entering-long-array-formulas-in-vba/
'Use the FormulaArray property of the Range object to enter array formulas
'in a cell from VBA. The array formula must be 255 characters or less or you’ll
'see the ever-helpful “Unable to set the FormulaArray property of the Range class” error.
'Solution: Split into parseable chunks
theFormulaPart1 = "=IF(MONTH(DATE(YEAR(B2),MONTH(B2),1))-" & _
"MONTH(DATE(YEAR(B2),MONTH(B2),1)-" & _
"(WEEKDAY(DATE(YEAR(B2),MONTH(B2),1))-1)+" & _
"{0;1;2;3;4;5}*7+{1,2,3,4,5,6,7}-1),""""," & _
"X_X_X())"
theFormulaPart2 = "DATE(YEAR(B2),MONTH(B2),1)-" & _
"(WEEKDAY(DATE(YEAR(B2),MONTH(B2),1))-1)+" & _
"{0;1;2;3;4;5}*7+{1,2,3,4,5,6,7}-1)"
With .Range("B4:H9")
.FormulaArray = theFormulaPart1
.Replace "X_X_X())", theFormulaPart2, LookAt:=xlPart, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.NumberFormat = "d"
End With
theFormulaPart1 = "=IF(MONTH(DATE(YEAR(J2),MONTH(J2),1))-" & _
"MONTH(DATE(YEAR(J2),MONTH(J2),1)-" & _
"(WEEKDAY(DATE(YEAR(J2),MONTH(J2),1))-1)+" & _
"{0;1;2;3;4;5}*7+{1,2,3,4,5,6,7}-1),""""," & _
"X_X_X())"
theFormulaPart2 = "DATE(YEAR(J2),MONTH(J2),1)-" & _
"(WEEKDAY(DATE(YEAR(J2),MONTH(J2),1))-1)+" & _
"{0;1;2;3;4;5}*7+{1,2,3,4,5,6,7}-1)"
With .Range("J4:P9")
.FormulaArray = theFormulaPart1
.Replace "X_X_X())", theFormulaPart2, LookAt:=xlPart, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
.NumberFormat = "d"
End With
'Tint Active Week Days
With .Range("B4:P9")
.Cells.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, _
Formula1:="=AND(B4>='" & sDataWksName & "'!$B$2,B4<='" & sDataWksName & "'!$B$2+4)"
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 16763904
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
'Format Calendar
With Range("B2:H9,J2:P9")
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
With .Borders
.LineStyle = xlContinuous
.Color = rgbLightGrey
.TintAndShade = 0
.Weight = xlThin
End With
End With
.Columns("A:Q").ColumnWidth = 4.67
End With
End Sub