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