fork download
  1. Option Explicit
  2.  
  3. Public Sub 関数設定()
  4. Dim ws As Worksheet
  5. Dim dd As Long
  6. Dim gp As Long
  7. Dim lastday As Long
  8. Dim yyyy As Long
  9. Dim mm As Long
  10. Dim wrow As Long
  11. Dim wcol As Long
  12. Dim wstr As String
  13. Set ws = ActiveSheet
  14. If GetVal(ws.Cells(2, "B").Value, 2000, 2099, yyyy) = False Then
  15. MsgBox ("年が不正です。")
  16. Exit Sub
  17. End If
  18. If GetVal(ws.Cells(3, "B").Value, 1, 12, mm) = False Then
  19. MsgBox ("月が不正です。")
  20. Exit Sub
  21. End If
  22. lastday = GetLastDay(yyyy, mm)
  23. For dd = 1 To lastday
  24. wrow = 3 + (dd - 1) * 3
  25. For gp = 1 To 4
  26. wcol = 10 + (gp - 1) * 5
  27. Call set_func(ws, wrow, wcol, wcol + 1, 1)
  28. Call set_func(ws, wrow, wcol, wcol + 2, 2)
  29. Call set_func(ws, wrow, wcol, wcol + 3, 2)
  30. Call set_func(ws, wrow, wcol, wcol + 4, 3)
  31. Next
  32. Next
  33. MsgBox ("完了")
  34. End Sub
  35. '関数設定
  36. Private Sub set_func(ByVal ws As Worksheet, ByVal trg_row As Long, ByVal ref_col As Long, ByVal trg_col As Long, ByVal count As Long)
  37. Dim rfcol As String
  38. Dim fmt As String
  39. Dim res As String
  40. Dim row1 As String
  41. Dim row2 As String
  42. Dim row3 As String
  43.  
  44. Const base As String = "=IFS(OR($JROW1=""午前①"",$JROW1=""午前②""),IF($HROW1>=COUNT,""出勤"","""")," & _
  45. "OR($JROW1=""午後①"",$JROW1=""午後②""),IF($HROW2>=COUNT,""出勤"","""")," & _
  46. "OR($JROW1=""夜間①"",$JROW1=""夜間②""),IF($HROW3>=COUNT,""出勤"","""")," & _
  47. "OR($JROW1=""待機①"",$JROW1=""待機②""),"""")"
  48. If IsError(ws.Cells(trg_row, trg_col).Value) = False Then
  49. If ws.Cells(trg_row, trg_col).Value <> "" Then
  50. If ws.Cells(trg_row, trg_col).HasFormula = False Then Exit Sub
  51. End If
  52. End If
  53. rfcol = ConvertToLetter(ref_col)
  54. row1 = trg_row
  55. row2 = trg_row + 1
  56. row3 = trg_row + 2
  57. fmt = base
  58. fmt = Replace(fmt, "J", rfcol)
  59. fmt = Replace(fmt, "ROW1", trg_row)
  60. fmt = Replace(fmt, "ROW2", trg_row + 1)
  61. fmt = Replace(fmt, "ROW3", trg_row + 2)
  62. fmt = Replace(fmt, "COUNT", count)
  63. ws.Cells(trg_row, trg_col).Formula = fmt
  64. End Sub
  65. '年/月取得
  66. Private Function GetVal(ByVal str As String, ByVal low_limit As Long, ByVal high_limit As Long, ByRef val As Long) As Boolean
  67. GetVal = False
  68. If IsNumeric(str) = False Then Exit Function
  69. val = CLng(str)
  70. If val < low_limit Then Exit Function
  71. If val > high_limit Then Exit Function
  72. GetVal = True
  73. End Function
  74.  
  75.  
  76. '列番号をA~Zの文字に変換
  77. Function ConvertToLetter(iCol As Long) As String
  78. Dim a As Long
  79. Dim b As Long
  80. a = iCol
  81. ConvertToLetter = ""
  82. Do While iCol > 0
  83. a = Int((iCol - 1) / 26)
  84. b = (iCol - 1) Mod 26
  85. ConvertToLetter = Chr(b + 65) & ConvertToLetter
  86. iCol = a
  87. Loop
  88. End Function
  89.  
  90. '月末日取得
  91. Private Function GetLastDay(ByVal yyyy As Long, ByVal mm As Long) As Long
  92. Dim wday As Date
  93. mm = mm + 1
  94. If mm > 12 Then
  95. yyyy = yyyy + 1
  96. mm = 1
  97. End If
  98. wday = DateSerial(yyyy, mm, 1)
  99. wday = wday - 1
  100. GetLastDay = Day(wday)
  101. End Function
  102.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty