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