进阶:使用VBA 如果你需要更高级和精确的控制,可以使用VBA来实现。以下是一个简单的VBA代码示例,可以生成随机考勤记录,排除周末和节假日: Sub GenerateAttendance() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Sheet1") Dim startDate As Date Dim endDate As Date Dim currentDate As Date Dim targetDays As Integer Dim attendanceDays As Integer Dim holidays As Range Dim isHoliday As Boolean Dim i As Integer startDate = DateValue("2024-06-01") endDate = DateValue("2024-06-30") targetDays = 20 ' 目标考勤天数 attendanceDays = 0 ' 假设E列是节假日列表 Set holidays = ws.Range("E1:E10") ' 清空之前的考勤记录 ws.Range("A2:B" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row).ClearContents i = 2 currentDate = startDate Do While currentDate <= endDate If Weekday(currentDate, vbMonday) <= 5 Then ' 排除周末 isHoliday = Application.WorksheetFunction.CountIf(holidays, currentDate) > 0 If Not isHoliday Then ' 排除节假日 ws.Cells(i, 1).Value = currentDate If attendanceDays < targetDays And Rnd() > 0.5 Then ws.Cells(i, 2).Value = "√" attendanceDays = attendanceDays + 1 Else ws.Cells(i, 2).Value = "" End If i = i + 1 End If End If currentDate = currentDate + 1 Loop End Sub 将上述代码复制到Excel的VBA编辑器中,然后运行该宏,它会生成一个随机考勤表,符合你的要求。