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("G15:M25").ClearContents '値をクリア
  18. maxrow1 = sh1.Cells(Rows.Count, "B").End(xlUp).Row 'B列最終行
  19. For row1 = 3 To maxrow1 '3行目~最終列まで
  20. If sh1.Rows(row1).Hidden = True Then GoTo NEXT99
  21. bun = sh1.Cells(row1, "U").Value '状況シートのU列分類2を選択
  22. idx = GetBunrui(bun) '分類を取得
  23. If idx < 0 Then
  24. MsgBox ("分類2不正")
  25. sh1.Select
  26. sh1.Cells(row1, "U").Select
  27. Exit Sub
  28. End If
  29. row2 = idx + 15 'B表 分類1毎の行番号
  30. idx = GetAge(sh1.Cells(row1, "E").Value) '年齢を取得
  31. col2 = idx + 8 'B表 年齢毎の列番号
  32. sh2.Cells(row2, col2).Value = sh2.Cells(row2, col2).Value + 1
  33. NEXT99:
  34. Next
  35. For row2 = 15 To 25
  36. rgs = "H" & row2 & ":M" & row2
  37. sh2.Cells(row2, "G").Formula = "=sum(" & rgs & ")"
  38. Next
  39. MsgBox ("完了")
  40. End Sub
  41.  
  42. Private Function GetBunrui(ByVal bun As String) As Long
  43. Dim buns As Variant
  44. Dim i As Long
  45. buns = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K")
  46. For i = 0 To UBound(buns)
  47. If bun = buns(i) Then
  48. GetBunrui = i
  49. Exit Function
  50. End If
  51. Next
  52. GetBunrui = -1
  53. End Function
  54.  
  55. Private Function GetAge(ByVal vage As Variant) As Long
  56. Dim vals As Variant
  57. Dim i As Long
  58. Dim age As Long
  59. vals = Array(15, 20, 25, 30, 35, 40)
  60. GetAge = UBound(vals)
  61. If IsNumeric(vage) = False Then Exit Function
  62. age = Int(vage)
  63. If age < vals(0) Or age >= vals(UBound(vals)) Then Exit Function
  64. For i = 0 To UBound(vals) - 1
  65. If age >= vals(i) And age < vals(i + 1) Then
  66. GetAge = i
  67. Exit Function
  68. End If
  69. Next
  70. End Function
  71.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty