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