fork download
  1. Option Explicit
  2.  
  3. Dim sh1 As Worksheet
  4. Dim dicT As Object '連想配列(祝日)
  5. Dim ro_person_tbl As Variant
  6. Dim hl_person_tbl As Variant
  7. Dim ro_sx As Long
  8. Dim hl_sx As Long
  9. Dim sdate As Date
  10.  
  11. Public Sub 当番表作成()
  12. Dim maxrow As Long
  13. Dim wrow As Long
  14. Dim sh2 As Worksheet
  15. Dim s_year As Variant
  16. Dim ro_type As String
  17. Dim ro_tbl As Variant
  18. Dim hl_tbl As Variant
  19. Dim ro_ix As Long
  20. Dim ro_sub_no As Variant
  21. Dim hl_sub_no As Variant
  22. Dim mm As Long
  23. Dim i As Long
  24. ro_tbl = Array("AABCDEF", "ABBCDEF", "ABCCDEF", "ABCDDEF", "ABCDEEF", "ABCDEFF", "ABCDEF")
  25. hl_tbl = Array("EACBEDF")
  26. Set dicT = CreateObject("Scripting.Dictionary")
  27. Set sh1 = Worksheets("当番表")
  28. Set sh2 = Worksheets("祝日")
  29. '祝日の記憶
  30. maxrow = sh2.Cells(Rows.count, 1).End(xlUp).row '最大行取得
  31. For wrow = 2 To maxrow
  32. dicT(sh2.Cells(wrow, 1).value) = True
  33. Next
  34. s_year = sh1.Cells(1, "D").value
  35. If s_year < 2020 Or s_year > 2099 Then
  36. sh1.Cells(1, "D").Select
  37. MsgBox ("指定年エラー")
  38. Exit Sub
  39. End If
  40. sh1.Cells(1, "K").Validation.Delete
  41. sh1.Cells(1, "K").Validation.Add Type:=xlValidateList, Operator:=xlBetween, Formula1:=Join(ro_tbl, ",")
  42. sh1.Cells(2, "K").value = hl_tbl(0)
  43. ro_type = sh1.Cells(1, "K").value
  44. ro_ix = GetRoIx(ro_type, ro_tbl)
  45. If ro_ix < 0 Then
  46. sh1.Cells(1, "K").Select
  47. MsgBox ("月~土ローテーションタイプエラー")
  48. Exit Sub
  49. End If
  50. ro_sub_no = sh1.Cells(1, "M").value
  51. hl_sub_no = sh1.Cells(2, "M").value
  52. If ro_sub_no < 1 Or ro_sub_no > Len(ro_type) Then
  53. sh1.Cells(1, "M").Select
  54. MsgBox ("月~土ローテーションタイプ内番目エラー")
  55. Exit Sub
  56. End If
  57. If hl_sub_no < 1 Or hl_sub_no > Len(hl_tbl(0)) Then
  58. sh1.Cells(2, "M").Select
  59. MsgBox ("祝祭日ローテーションタイプ内番目エラー")
  60. Exit Sub
  61. End If
  62. ro_person_tbl = create_person_table(ro_tbl)
  63. hl_person_tbl = create_person_table(hl_tbl)
  64. '開始インデックスの設定
  65. ro_sx = ro_ix * 7 + ro_sub_no - 1
  66. hl_sx = hl_sub_no - 1
  67. '4行目以降をクリア
  68. sh1.Rows("4:" & Rows.count).ClearContents
  69. sdate = DateSerial(s_year, 1, 1)
  70. '1~12月の当番表作成
  71. For mm = 1 To 12
  72. Call make_1month(mm)
  73. Next
  74. MsgBox ("完了")
  75. End Sub
  76. '1カ月分の当番表作成
  77. Private Sub make_1month(ByVal mm As Long)
  78. Dim wrow As Long
  79. Dim wcol As Long
  80. Dim lastday As Long
  81. Dim dd As Long
  82. Dim week As Long
  83. wrow = (mm - 1) * 4 + 4
  84. sh1.Cells(wrow, 1).value = mm & "月"
  85. lastday = Day(DateSerial(year(sdate), mm + 1, 0))
  86. sh1.Range("B" & wrow + 1 & ":AF" & wrow + 1).Font.ColorIndex = xlAutomatic
  87. For dd = 1 To lastday
  88. wcol = dd + 1
  89. 'sh1.Cells(wrow, wcol).value = sdate
  90. sh1.Cells(wrow, wcol).value = Day(sdate) & "日"
  91. week = Weekday(sdate)
  92. sh1.Cells(wrow + 1, wcol).value = WeekdayName(week, True)
  93. If week = 1 Or dicT(sdate) = True Then
  94. sh1.Cells(wrow + 1, wcol).Font.Color = -16776961
  95. End If
  96. If week = 1 Or dicT(sdate) = True Then
  97. sh1.Cells(wrow + 2, wcol).value = getPerson(hl_sx, hl_person_tbl)
  98. Else
  99. sh1.Cells(wrow + 2, wcol).value = getPerson(ro_sx, ro_person_tbl)
  100. End If
  101. sdate = sdate + 1
  102. Next
  103. End Sub
  104. '当番取得
  105. Private Function getPerson(ByRef ix As Long, ByRef person_tbl As Variant)
  106. getPerson = person_tbl(ix)
  107. ix = ix + 1
  108. If ix > UBound(person_tbl) Then
  109. ix = 0
  110. End If
  111. End Function
  112. 'ローテーション・インデックス取得
  113. Private Function GetRoIx(ByVal ro_type As String, ByVal ro_tbl As Variant) As Long
  114. Dim i As Long
  115. GetRoIx = -1
  116. For i = 0 To UBound(ro_tbl)
  117. If ro_type = ro_tbl(i) Then
  118. GetRoIx = i
  119. Exit Function
  120. End If
  121. Next
  122. End Function
  123. '個人の通しテーブル作成
  124. Private Function create_person_table(ByVal rotbl As Variant) As Variant
  125. Dim i As Long
  126. Dim j As Long
  127. Dim k As Long
  128. Dim otbl As Variant
  129. Dim str As String
  130. otbl = Array("")
  131. k = 0
  132. For i = 0 To UBound(rotbl)
  133. str = rotbl(i)
  134. For j = 1 To Len(str)
  135. ReDim Preserve otbl(k)
  136. otbl(k) = Mid(str, j, 1)
  137. k = k + 1
  138. Next
  139. Next
  140. create_person_table = otbl
  141. End Function
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty