fork download
  1. Option Explicit
  2.  
  3. Public Sub 滞在人数集計()
  4. Dim ws1 As Worksheet '滞在予定
  5. Dim ws2 As Worksheet '集計結果
  6. Dim lastRow As Long
  7. Dim row1 As Long '滞在予定の行番号
  8. Dim row2 As Long '集計結果の行番号
  9. Dim start_date As Date '集計開始日
  10. Dim end_date As Date '集計終了日
  11. Dim wdate As Date
  12. Set ws1 = Worksheets("滞在予定")
  13. Set ws2 = Worksheets("集計結果")
  14. lastRow = ws1.Cells(Rows.count, "C").End(xlUp).Row 'C列最終行取得
  15. If lastRow < 3 Then
  16. MsgBox ("滞在者なし")
  17. Exit Sub
  18. End If
  19. '集計開始日/終了日をチェックし、開始日/終了日を取得する
  20. Call check_shukei_date(ws1.Cells(1, "AG"), ws1.Cells(2, "AG"), start_date, end_date)
  21. '集計結果クリア
  22. ws2.Cells.ClearContents
  23. ws2.Cells(1, "A").Value = "日付"
  24. ws2.Cells(1, "B").Value = "人数"
  25. '集計開始日~集計終了日設定
  26. row2 = 2
  27. For wdate = start_date To end_date
  28. ws2.Cells(row2, "A").Value = wdate
  29. ws2.Cells(row2, "B").Value = 0
  30. row2 = row2 + 1
  31. Next
  32. '3行~最終行まで繰り返す
  33. For row1 = 3 To lastRow
  34. '滞在人数設定
  35. Call set_taizai_ninzu(ws1, row1, start_date, end_date, ws2)
  36. Next
  37. MsgBox ("完了")
  38. End Sub
  39. '集計日チェック
  40. Private Sub check_shukei_date(ByRef rng_s As Range, ByRef rng_e As Range, ByRef s_d As Date, ByRef e_d As Date)
  41. Call check_date(rng_s, False, "開始日") '開始日チェック(空白の日付を許さない)
  42. Call check_date(rng_e, False, "終了日") '終了日チェック(空白の日付を許さない)
  43. If rng_s.Value > rng_e.Value Then
  44. Call abort(rng_e, "終了日順序不正")
  45. End If
  46. s_d = rng_s.Value
  47. e_d = rng_e.Value
  48. End Sub
  49.  
  50. '滞在人数設定
  51. Private Sub set_taizai_ninzu(ByRef ws1 As Worksheet, ByVal row1 As Long, ByVal s_d As Date, ByVal e_d As Date, ByRef ws2 As Worksheet)
  52. Dim sdate As Date '滞在者の滞在開始日
  53. Dim edate As Date '滞在者の滞在終了日
  54. Dim wdate As Date
  55. Dim row2 As Long '集計結果の行番号
  56. '前泊の文字チェック
  57. If ws1.Cells(row1, "D").Value <> "" And ws1.Cells(row1, "D").Value <> "前泊" Then
  58. Call abort(ws1.Cells(row1, "D"), "前泊以外の文字")
  59. End If
  60. Call check_date(ws1.Cells(row1, "E"), True, "出発日") '出発日チェック(空白の日付を許す)
  61. Call check_date(ws1.Cells(row1, "F"), True, "帰宅日") '出発日チェック(空白の日付を許す)
  62. sdate = ws1.Cells(row1, "E").Value
  63. edate = ws1.Cells(row1, "F").Value
  64. '出発日、帰宅日の何れかが空白ならカウントしない
  65. If sdate = 0 Or edate = 0 Then Exit Sub
  66. '出発日<帰宅日であることのチェック
  67. If sdate >= edate Then
  68. Call abort(ws1.Cells(row1, "F"), "帰宅日順序不正")
  69. End If
  70. '前泊なら翌日からカウントする
  71. If ws1.Cells(row1, "D").Value = "前泊" Then
  72. sdate = sdate + 1
  73. End If
  74. '滞在開始日~滞在終了日まで繰り返す
  75. For wdate = sdate To edate
  76. '当日が集計開始~集計終了日の範囲内であればカウントする
  77. If wdate >= s_d And wdate <= e_d Then
  78. '集計結果の行番号を算出する
  79. row2 = wdate - s_d + 2
  80. '算出した行に1加算する
  81. ws2.Cells(row2, "B").Value = ws2.Cells(row2, "B").Value + 1
  82. End If
  83. Next
  84. End Sub
  85. '日付チェック
  86. Private Sub check_date(ByRef rng As Range, ByVal allow_space As Boolean, ByVal base_msg As String)
  87. If rng.Value = "" Then
  88. If allow_space = True Then Exit Sub
  89. Call abort(rng, base_msg & "が空白")
  90. Else
  91. If IsDate(rng.Value) = False Then
  92. Call abort(rng, base_msg & "が不正")
  93. End If
  94. End If
  95. End Sub
  96. 'アボート処理
  97. Private Sub abort(ByRef rng As Range, ByVal msg As String)
  98. rng.Parent.Activate
  99. rng.Select
  100. MsgBox (msg)
  101. End
  102. End Sub
  103.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty