안녕하세요한셀 vba로 근무표를 짤려고 하는데매크로 자체에는 오류가 없고 만들어는지는데근무표에 사람 이름이 안뜨고 자꾸 편성실패라고만 뜹니다대체 왜그럴까요1 Option Explicit 2 3 Dim timeSlots As Variant 4 Dim used() As Long 5 6 Sub 한달_근무표_생성() 7 Call 초기화시간대 8 Call 시트초기화 9 Call GenerateMonthlySchedule(DateSerial(Year(Date), Month(Date), 1)) 10 End Sub 11 12 Sub 초기화시간대() 13 timeSlots = Array("08-10", "10-12", "12-14", "14-16", "16-18", "야간") 14 End Sub 15 16 Sub 시트초기화() 17 Dim ws1 As Worksheet, ws2 As Worksheet 18 On Error Resume Next 19 Application.DisplayAlerts = False 20 Worksheets("근무표").Delete 21 Worksheets("카운트").Delete 22 On Error GoTo 0 23 Application.DisplayAlerts = True 24 Set ws1 = Worksheets.Add: ws1.Name = "근무표" 25 Set ws2 = Worksheets.Add: ws2.Name = "카운트" 26 End Sub 27 28 Sub GenerateMonthlySchedule(startDate As Date) 29 Dim wsInfo As Worksheet, wsDuty As Worksheet, wsCount As Worksheet 30 Dim i As Long, j As Long, d As Long, k As Long 31 Dim totalDays As Long: totalDays = Day(DateSerial(Year(startDate), Month(startDate) + 1, 0)) 32 Dim names() As String 33 Dim leaveStart() As Date, leaveEnd() As Date 34 Dim dutyCount() As Long 35 Dim dutyMatrix() As Long 36 Dim lastRow As Long, validCount As Long 37 Dim restDay() As Boolean 38 Dim today As Date 39 40 Set wsInfo = Worksheets("인원정보") 41 Set wsDuty = Worksheets("근무표") 42 Set wsCount = Worksheets("카운트") 43 44 lastRow = wsInfo.Cells(Rows.Count, 1).End(xlUp).Row 45 validCount = 0 46 ReDim names(1 To lastRow - 1) 47 ReDim leaveStart(1 To lastRow - 1) 48 ReDim leaveEnd(1 To lastRow - 1) 49 50 For i = 2 To lastRow 51 If Trim(wsInfo.Cells(i, 1).Value) <> "" And LCase(Trim(wsInfo.Cells(i, 2).Value)) = "정상" Then 52 validCount = validCount + 1 53 names(validCount) = Trim(wsInfo.Cells(i, 1).Value) 54 If IsDate(wsInfo.Cells(i, 3).Value) Then leaveStart(validCount) = wsInfo.Cells(i, 3).Value Else leaveStart(validCount) = 0 55 If IsDate(wsInfo.Cells(i, 4).Value) Then leaveEnd(validCount) = wsInfo.Cells(i, 4).Value Else leaveEnd(validCount) = 0 56 End If 57 Next i 58 59 ReDim Preserve names(1 To validCount) 60 ReDim Preserve leaveStart(1 To validCount) 61 ReDim Preserve leaveEnd(1 To validCount) 62 ReDim dutyCount(1 To validCount) 63 ReDim restDay(1 To validCount) 64 ReDim used(1 To validCount) 65 ReDim dutyMatrix(1 To validCount, 1 To UBound(timeSlots) + 1) 66 67 wsDuty.Cells(1, 1).Value = "날짜" 68 For j = 0 To UBound(timeSlots) 69 wsDuty.Cells(1, j + 2).Value = timeSlots(j) 70 Next j 71 72 For d = 0 To totalDays - 1 73 today = startDate + d 74 wsDuty.Cells(d + 2, 1).Value = Format(today, "yyyy-mm-dd") 75 76 ' ★ 하루 시작할 때 used 초기화 77 For i = 1 To validCount 78 used(i) = 0 79 Next i 80 81 ' 야간 근무 배정 82 Dim nightCandidates As Collection: Set nightCandidates = New Collection 83 For i = 1 To validCount 84 If Not restDay(i) And Not IsDateInRange(today, leaveStart(i), leaveEnd(i)) Then nightCandidates.Add i 85 Next i 86 87 If nightCandidates.Count > 0 Then 88 k = FindLeastUsed(dutyCount, nightCandidates) 89 If k > 0 And k <= UBound(names) Then 90 wsDuty.Cells(d + 2, 7).Value = names(k) 91 dutyCount(k) = dutyCount(k) + 1 92 dutyMatrix(k, 6) = dutyMatrix(k, 6) + 1 93 restDay(k) = True 94 End If 95 End If 96 97 ' 주간 근무 배정 98 For j = 0 To 4 99 Dim dayCandidates As Collection: Set dayCandidates = New Collection100 For i = 1 To validCount101 If used(i) < 2 And restDay(i) = False And Not IsDateInRange(today, leaveStart(i), leaveEnd(i)) Then102 dayCandidates.Add i103 End If104 Next i105 If dayCandidates.Count > 0 Then106 k = FindLeastUsed(dutyCount, dayCandidates)107 If k > 0 And k <= UBound(names) Then108 dutyMatrix(k, j + 1) = dutyMatrix(k, j + 1) + 1109 wsDuty.Cells(d + 2, j + 2).Value = names(k)110 dutyCount(k) = dutyCount(k) + 1111 used(k) = used(k) + 1112 End If113 End If114 Next j115 116 For i = 1 To validCount117 restDay(i) = False118 Next i119 Next d120 121 ' 카운트 시트 작성122 wsCount.Cells(1, 1).Value = "이름"123 For j = 0 To UBound(timeSlots)124 wsCount.Cells(1, j + 2).Value = timeSlots(j)125 Next j126 wsCount.Cells(1, UBound(timeSlots) + 3).Value = "총근무수"127 128 For i = 1 To validCount129 wsCount.Cells(i + 1, 1).Value = names(i)130 Dim total As Long: total = 0131 For j = 1 To UBound(timeSlots) + 1132 wsCount.Cells(i + 1, j + 1).Value = dutyMatrix(i, j)133 total = total + dutyMatrix(i, j)134 Next j135 wsCount.Cells(i + 1, UBound(timeSlots) + 3).Value = total136 Next i137 138 ' 근무표 시트 서식139 Dim rng As Range140 Set rng = wsDuty.Range("A1:G" & (totalDays + 1))141 With rng142 .Font.Name = "맑은 고딕"143 .Font.Size = 10144 .HorizontalAlignment = xlCenter145 .VerticalAlignment = xlCenter146 .Columns.AutoFit147 .Borders.LineStyle = xlContinuous148 End With149 wsDuty.Rows(1).Font.Bold = True150 wsDuty.Rows(1).Interior.Color = RGB(220, 220, 220)151 End Sub152 153 Function IsDateInRange(day As Date, startDate As Date, endDate As Date) As Boolean154 If startDate = 0 Or endDate = 0 Then155 IsDateInRange = False156 Else157 IsDateInRange = (day >= startDate And day <= endDate)158 End If159 End Function160 161 Function FindLeastUsed(counts() As Long, candidates As Collection) As Long162 Dim minVal As Long: minVal = 9999163 Dim minIdx As Long: minIdx = -1164 Dim i As Long165 Dim idx As Long166 167 For i = 1 To candidates.Count168 idx = CLng(candidates(i))169 If idx >= 1 And idx <= UBound(counts) Then170 If counts(idx) < minVal Then171 minVal = counts(idx)172 minIdx = idx173 End If174 End If175 Next i176 177 FindLeastUsed = minIdx178 End Function
음... 근무표에 이름이 안 뜨고 "편성 실패"라는 메시지도 안 뜬다면 몇 가지 가능성이 있어요.코드 자체에는 오류가 없다고 하셨으니, 데이터나 로직 상의 문제일 가능성이 높아 보입니다.
1. "인원정보" 시트 확인: "인원정보" 시트에정상 근무자(B열 값이 "정상")가 있는지, 그리고 이름(A열)과 휴가 시작/종료일(C열, D열)이 제대로 입력되어 있는지 꼼꼼히 확인해보세요. 빈 셀이나 잘못된 데이터 형식이 있는지도 살펴보시고요.특히 날짜는 엑셀에서 날짜 형식으로 제대로 인식되는지 확인하는게 중요해요.
2. validCount 값 확인: 디버깅 모드(F8)로 코드를 한 줄씩 실행하면서 57번 라인 Next i다음에validCount 값이 0보다 큰지 확인해보세요. 만약 0이라면, 조건에 맞는 정상 근무자가 없다는 뜻이므로 위의 1번을 다시 확인해야 합니다.
3. dayCandidates, nightCandidates 컬렉션 확인: 96번 라인, 114번 라인 근처에서 dayCandidates와 nightCandidates 컬렉션에 실제로 사람 인덱스(i)가 추가되는지 확인해보세요. 만약 조건에 맞는 사람이 없어서 추가가 안된다면 근무표에 이름이 들어갈 수 없겠죠.휴가 기간이나 restDay 변수 때문에 후보에서 제외되는 경우가 많은지 살펴보세요.
4. FindLeastUsed 함수 확인: FindLeastUsed 함수가 제대로 작동하는지 확인해보세요. 이 함수는 가장 적게 근무한 사람의 인덱스를 반환해야 하는데, 혹시 -1을 반환해서 이름이 제대로 배정되지 않는 것일 수도 있습니다.
5. GenerateMonthlySchedule 시작 날짜: GenerateMonthlySchedule 함수를 호출할 때 넘기는 날짜(DateSerial(Year(Date), Month(Date), 1))가 예상대로 현재 달의 1일인지 확인해보세요.혹시 다른 날짜로 설정되어 있다면 의도치 않은 결과가 나올 수 있습니다.
위의 사항들을 확인해보시고, 그래도 문제가 해결되지 않으면, "인원정보" 시트의 데이터 예시와 "편성 실패" 메시지가 언제 어떻게 표시되는지 알려주시면 더 정확하게 답변드릴 수 있을 것 같아요."근무표" 시트에 어떤 값들이 표시되는지도 알려주시면 도움이 될 것 같습니다.
AI 분석 및 채팅
갤러리