fork download
  1. Option Explicit
  2.  
  3. Public Sub 年齢別カウント_横()
  4. Dim sh1 As Worksheet
  5. Dim sh2 As Worksheet
  6. Dim maxrow1 As Long 'B列最終行
  7. Dim row1 As Long
  8. Dim row2 As Long
  9. Dim col2 As Long
  10. Dim rgs As String
  11. Dim rgs1 As String
  12. Dim rgs2 As String
  13. Dim idx As Long
  14. Dim bun As String
  15. Set sh1 = Worksheets("状況")
  16. Set sh2 = Worksheets("B表")
  17. sh2.Range("G6:M9").ClearContents '値をクリア
  18. maxrow1 = sh1.Cells(Rows.Count, "B").End(xlUp).Row 'B列最終行
  19. For row1 = 5 To maxrow1 '8行目~最終列まで
  20. bun = sh1.Cells(row1, "C").Value '状況シートのC列分類1を選択
  21. idx = GetBunrui(bun) '分類を取得
  22. If idx < 0 Then
  23. MsgBox ("分類1不正")
  24. sh1.Select
  25. sh1.Cells(row1, "C").Select
  26. Exit Sub
  27. End If
  28. row2 = idx + 6 'B表 分類1毎の行番号
  29. idx = GetAge(sh1.Cells(row1, "E").Value) '年齢を取得
  30. col2 = idx + 8 'B表 年齢毎の列番号
  31. sh2.Cells(row2, col2).Value = sh2.Cells(row2, col2).Value + 1
  32. Next
  33. For row2 = 6 To 9
  34. rgs = "H" & row2 & ":M" & row2
  35. sh2.Cells(row2, "G").Formula = "=sum(" & rgs & ")"
  36. Next
  37. MsgBox ("完了")
  38. End Sub
  39.  
  40. Private Function GetBunrui(ByVal bun As String) As Long
  41. Dim buns As Variant
  42. Dim i As Long
  43. buns = Array("A", "B", "C", "D")
  44. For i = 0 To UBound(buns)
  45. If bun = buns(i) Then
  46. GetBunrui = i
  47. Exit Function
  48. End If
  49. Next
  50. GetBunrui = -1
  51. End Function
  52.  
  53. Private Function GetAge(ByVal vage As Variant) As Long
  54. Dim vals As Variant
  55. Dim i As Long
  56. Dim age As Long
  57. vals = Array(15, 20, 25, 30, 35, 40)
  58. GetAge = UBound(vals)
  59. If IsNumeric(vage) = False Then Exit Function
  60. age = Int(vage)
  61. If age < vals(0) Or age >= vals(UBound(vals)) Then Exit Function
  62. For i = 0 To UBound(vals) - 1
  63. If age >= vals(i) And age < vals(i + 1) Then
  64. GetAge = i
  65. Exit Function
  66. End If
  67. Next
  68. End Function
  69.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty