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.  
  49. If ws.Cells(trg_row, trg_col).Value <> "" Then Exit Sub
  50. rfcol = ConvertToLetter(ref_col)
  51. row1 = trg_row
  52. row2 = trg_row + 1
  53. row3 = trg_row + 2
  54. fmt = base
  55. fmt = Replace(fmt, "J", rfcol)
  56. fmt = Replace(fmt, "ROW1", trg_row)
  57. fmt = Replace(fmt, "ROW2", trg_row + 1)
  58. fmt = Replace(fmt, "ROW3", trg_row + 2)
  59. fmt = Replace(fmt, "COUNT", count)
  60. ws.Cells(trg_row, trg_col).Formula = fmt
  61. End Sub
  62. '年/月取得
  63. Private Function GetVal(ByVal str As String, ByVal low_limit As Long, ByVal high_limit As Long, ByRef val As Long) As Boolean
  64. GetVal = False
  65. If IsNumeric(str) = False Then Exit Function
  66. val = CLng(str)
  67. If val < low_limit Then Exit Function
  68. If val > high_limit Then Exit Function
  69. GetVal = True
  70. End Function
  71.  
  72.  
  73. '列番号をA~Zの文字に変換
  74. Function ConvertToLetter(iCol As Long) As String
  75. Dim a As Long
  76. Dim b As Long
  77. a = iCol
  78. ConvertToLetter = ""
  79. Do While iCol > 0
  80. a = Int((iCol - 1) / 26)
  81. b = (iCol - 1) Mod 26
  82. ConvertToLetter = Chr(b + 65) & ConvertToLetter
  83. iCol = a
  84. Loop
  85. End Function
  86.  
  87. '月末日取得
  88. Private Function GetLastDay(ByVal yyyy As Long, ByVal mm As Long) As Long
  89. Dim wday As Date
  90. mm = mm + 1
  91. If mm > 12 Then
  92. yyyy = yyyy + 1
  93. mm = 1
  94. End If
  95. wday = DateSerial(yyyy, mm, 1)
  96. wday = wday - 1
  97. GetLastDay = Day(wday)
  98. End Function
  99.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty