fork download
  1. Option Explicit
  2. Const Week_str As String = "*,日,月,火,水,木,金,土"
  3. Const Shift_str As String = "1,1,1,1,ヤ,2,2,2,2,ヤ,3,3,3,3,ヤ,ヤ"
  4. Const A_stx As Long = 6 'Aグループの2020年1月1日のインデックス
  5. Const B_stx As Long = 2 'Bグループの2020年1月1日のインデックス
  6. Const C_stx As Long = 14 'Cグループの2020年1月1日のインデックス
  7. Const D_stx As Long = 10 'Dグループの2020年1月1日のインデックス
  8. Dim ws As Worksheet
  9. Dim cs As Worksheet
  10. Dim shift_p As Variant
  11. Dim week_name As Variant
  12. Dim dicT As Object '祝日カレンダーの祝日を記憶
  13. Public Sub シフト表作成()
  14. Dim yyyy As Long
  15. Dim mm As Long
  16. Dim maxrow As Long
  17. Dim wrow As Long
  18. Set dicT = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  19. shift_p = Split(Shift_str, ",")
  20. week_name = Split(Week_str, ",")
  21. Set ws = Worksheets("シフト表")
  22. Set cs = Worksheets("祝日")
  23. yyyy = 0
  24. If IsNumeric(ws.Cells(1, "I").Value) = True Then
  25. If ws.Cells(1, "I").Value >= 2020 And ws.Cells(1, "I").Value <= 2099 Then
  26. yyyy = ws.Cells(1, "I").Value
  27. End If
  28. End If
  29. If yyyy = 0 Then
  30. MsgBox ("2020年~2099年を指定してください")
  31. Exit Sub
  32. End If
  33. maxrow = cs.Cells(Rows.Count, 1).End(xlUp).row '最終行を求める
  34. '祝日カレンダーを記憶
  35. For wrow = 2 To maxrow
  36. dicT(cs.Cells(wrow, "A").Value) = True
  37. Next
  38. ws.Range("A3:AF87").ClearContents
  39. For mm = 1 To 12
  40. Call make_1month(yyyy, mm)
  41. Next
  42.  
  43. End Sub
  44. '1カ月分のカレンダー作成
  45. Private Sub make_1month(ByVal yyyy As Long, ByVal mm As Long)
  46. Dim diff As Long
  47. Dim aix As Long
  48. Dim bix As Long
  49. Dim cix As Long
  50. Dim dix As Long
  51. Dim dd As Long
  52. Dim wdate As Date
  53. Dim lastday As Long '月末日
  54. Dim srow As Long '開始行(曜日)
  55. Dim wx As Long
  56. Dim wcol As Long
  57. lastday = Day(DateSerial(yyyy, mm + 1, 0))
  58. diff = DateSerial(yyyy, mm, 1) - DateSerial(2020, 1, 1)
  59. aix = (diff + A_stx) Mod 16
  60. bix = (diff + B_stx) Mod 16
  61. cix = (diff + C_stx) Mod 16
  62. dix = (diff + D_stx) Mod 16
  63. srow = (mm - 1) * 7 + 3
  64. ws.Cells(srow, "A").Value = mm & "月"
  65. ws.Cells(srow + 2, "A").Value = "A"
  66. ws.Cells(srow + 3, "A").Value = "B"
  67. ws.Cells(srow + 4, "A").Value = "C"
  68. ws.Cells(srow + 5, "A").Value = "D"
  69.  
  70. wdate = DateSerial(yyyy, mm, 1)
  71. '1~月末まで繰り返す
  72. For dd = 1 To lastday
  73. wx = Weekday(wdate, vbSunday)
  74. wcol = dd + 1
  75. ws.Cells(srow, wcol).Value = week_name(wx) '曜日
  76. '祝日か日曜日なら赤
  77. ws.Cells(srow, wcol).Font.ColorIndex = xlAutomatic
  78. If dicT.exists(wdate) = True Or wx = 1 Then
  79. ws.Cells(srow, wcol).Font.Color = -16776961
  80. '土曜日なら青
  81. ElseIf wx = 7 Then
  82. ws.Cells(srow, wcol).Font.Color = -1003520
  83. End If
  84. ws.Cells(srow + 1, wcol).Value = dd & "日" '日
  85. ws.Cells(srow + 2, wcol).Value = shift_p(aix) 'Aのシフト
  86. ws.Cells(srow + 3, wcol).Value = shift_p(bix) 'Bのシフト
  87. ws.Cells(srow + 4, wcol).Value = shift_p(cix) 'Cのシフト
  88. ws.Cells(srow + 5, wcol).Value = shift_p(dix) 'Dのシフト
  89. aix = next_shift(aix)
  90. bix = next_shift(bix)
  91. cix = next_shift(cix)
  92. dix = next_shift(dix)
  93. wdate = wdate + 1
  94. Next
  95. End Sub
  96. '次のシフトを取得
  97. Private Function next_shift(ByVal shift_ix As Long) As Long
  98. next_shift = shift_ix + 1
  99. If next_shift > 15 Or next_shift < 0 Then
  100. next_shift = 0
  101. End If
  102. End Function
  103.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty