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 = 17 + (gp - 1) * 5
  27. Call set_func(ws, wrow, wcol, wcol + 1, 1, "F", "Ⅰ棟")
  28. Call set_func(ws, wrow, wcol, wcol + 2, 2, "I", "Ⅱ棟-2F")
  29. Call set_func(ws, wrow, wcol, wcol + 3, 2, "I", "Ⅱ棟-3F")
  30. Call set_func(ws, wrow, wcol, wcol + 4, 3, "O", "Ⅲ棟")
  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, ByVal HCOL As String, ByVal POS As String)
  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, "H", HCOL)
  59. fmt = Replace(fmt, "出勤", POS)
  60. fmt = Replace(fmt, "J", rfcol)
  61. fmt = Replace(fmt, "ROW1", trg_row)
  62. fmt = Replace(fmt, "ROW2", trg_row + 1)
  63. fmt = Replace(fmt, "ROW3", trg_row + 2)
  64. fmt = Replace(fmt, "COUNT", count)
  65. ws.Cells(trg_row, trg_col).Formula = fmt
  66. End Sub
  67. '年/月取得
  68. Private Function GetVal(ByVal str As String, ByVal low_limit As Long, ByVal high_limit As Long, ByRef val As Long) As Boolean
  69. GetVal = False
  70. If IsNumeric(str) = False Then Exit Function
  71. val = CLng(str)
  72. If val < low_limit Then Exit Function
  73. If val > high_limit Then Exit Function
  74. GetVal = True
  75. End Function
  76.  
  77.  
  78. '列番号をA~Zの文字に変換
  79. Function ConvertToLetter(iCol As Long) As String
  80. Dim a As Long
  81. Dim b As Long
  82. a = iCol
  83. ConvertToLetter = ""
  84. Do While iCol > 0
  85. a = Int((iCol - 1) / 26)
  86. b = (iCol - 1) Mod 26
  87. ConvertToLetter = Chr(b + 65) & ConvertToLetter
  88. iCol = a
  89. Loop
  90. End Function
  91.  
  92. '月末日取得
  93. Private Function GetLastDay(ByVal yyyy As Long, ByVal mm As Long) As Long
  94. Dim wday As Date
  95. mm = mm + 1
  96. If mm > 12 Then
  97. yyyy = yyyy + 1
  98. mm = 1
  99. End If
  100. wday = DateSerial(yyyy, mm, 1)
  101. wday = wday - 1
  102. GetLastDay = day(wday)
  103. End Function
  104.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty