By: Afia Kona

In reply to Sam.

Dear Sam,
Thank you for your comment. I understand that you are looking for a more dynamic Attendance Sheet, if so, you can use the following VBA code as an update:

Private Sub Worksheet_Change(ByVal Target As Range)

'Developed by ExcelDemy

If Target.Address = "$C$4" Or Target.Address = "$C$5" Then

totalDays = Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

If Range("C5").Value Mod 4 = 0 Then
totalDays(1) = 29
End If

Dim specificDate As Date
Dim dayOfWeek As Integer
Dim dayName As String

monthNumber = Month(DateValue(Range("C4").Value & " 1 2000"))
yearNumber = Range("C5").Value

Range(Range("D7").Cells(1, 1), Range("D7").Cells(6, 31 + 2)).ClearContents
Range(Range("D7").Cells(1, 29), Range("D7").Cells(6, 31 + 2)).ClearFormats

For i = 1 To totalDays(monthNumber - 1)

' Specify the date
specificDate = DateSerial(yearNumber, monthNumber, i)

' Get the day of the week as a number (1 for Sunday, 2 for Monday, etc.)
dayOfWeek = Weekday(specificDate, vbSunday)

' Get the name of the day of the week
dayName = WeekdayName(dayOfWeek, False, vbSunday)

' Display the name of the day
Range("D7").Cells(1, i) = i
Range("D8").Cells(1, i) = Left(dayName, 3)

Next i

Range("D7:D12").Copy
For i = 29 To totalDays(monthNumber - 1)
Range("D7:D12").Cells(1, i).PasteSpecial Paste:=xlPasteFormats
Range("D7:D12").Cells(1, i).ColumnWidth = Range("D7").Cells(1, 1).ColumnWidth
Next i

Application.CutCopyMode = False

Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 1) = "Present"
Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 1).Interior.Color = vbGreen
Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 1).ColumnWidth = 8
Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 1).Font.Bold = True
Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 1).HorizontalAlignment = xlHAlignCenter
Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 1).VerticalAlignment = xlVAlignCenter

Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 2) = "Absent"
Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 2).Interior.Color = vbGreen
Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 2).ColumnWidth = 8
Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 2).Font.Bold = True
Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 2).HorizontalAlignment = xlHAlignCenter
Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 2).VerticalAlignment = xlVAlignCenter

For j = 1 To 2
For k = 1 To 5
With Range("D7:D12").Cells(2, totalDays(monthNumber - 1) + 1).Cells(k, j)
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeTop).Color = RGB(0, 0, 0)
.Borders(xlEdgeTop).Weight = xlThin

.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Color = RGB(0, 0, 0)
.Borders(xlEdgeBottom).Weight = xlThin

.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Color = RGB(0, 0, 0)
.Borders(xlEdgeLeft).Weight = xlThin

.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Color = RGB(0, 0, 0)
.Borders(xlEdgeRight).Weight = xlThin
End With
Next k
Next j
End If
End Sub

Download this Excel file for a better understanding.
I hope that your problem will be solved now. If you have any further issue, please let us know in the comment section.

Best
Afia Aziz Kona
Excel and VBA Content Developer
Exceldemy