fork download
  1. Option Explicit
  2. Public Sub 品名別集計()
  3. Dim sh1 As Worksheet
  4. Dim sh2 As Worksheet
  5. Dim dicH As Object
  6. Dim dicD As Object
  7. Dim maxrow1 As Long
  8. Dim maxrow2 As Long
  9. Dim maxcol1 As Long
  10. Dim maxcol2 As Long
  11. Dim row1 As Long
  12. Dim row2 As Long
  13. Dim col1 As Long
  14. Dim col2 As Long
  15. Dim keyc As String
  16. Dim keyr As String
  17.  
  18. Set dicH = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  19. Set dicD = CreateObject("Scripting.Dictionary") ' 連想配列の定義
  20. Set sh1 = Worksheets("sheet1")
  21. Set sh2 = Worksheets("sheet2")
  22. maxrow1 = sh1.Cells(Rows.Count, "B").End(xlUp).Row 'sheet1 最終行を求める
  23. maxrow2 = sh2.Cells(Rows.Count, "B").End(xlUp).Row 'sheet2 最終行を求める
  24. maxcol1 = sh1.Cells(4, Columns.Count).End(xlToLeft).Column 'Sheet1 最終列を求める
  25. maxcol2 = sh2.Cells(2, Columns.Count).End(xlToLeft).Column 'Sheet2 最終列を求める
  26. If maxrow1 < 5 Then Exit Sub
  27. If maxrow2 < 2 Then Exit Sub
  28. If maxcol1 < 3 Then Exit Sub
  29. If maxcol2 < 3 Then Exit Sub
  30. For col2 = 3 To maxcol2
  31. keyc = sh2.Cells(2, col2).Value
  32. dicH(keyc) = col2
  33. Next
  34. For row2 = 3 To maxrow2
  35. keyr = sh2.Cells(row2, "B").Value
  36. dicD(keyr) = row2
  37. Next
  38. For col1 = 3 To maxcol1
  39. keyc = sh1.Cells(2, col1).Value
  40. If keyc <> "" Then
  41. If dicH.exists(keyc) = False Then
  42. MsgBox (keyc & "はSheet2に存在しません")
  43. sh1.Activate
  44. sh1.Cells(2, col1).Select
  45. Exit Sub
  46. End If
  47. End If
  48. Next
  49. For row1 = 5 To maxrow1
  50. For col1 = 3 To maxcol1
  51. keyc = sh1.Cells(2, col1).Value
  52. keyr = sh1.Cells(row1, "B").Value
  53. If dicD.exists(keyr) = False Then
  54. MsgBox (keyr & "はSheet2に存在しません")
  55. sh1.Activate
  56. sh1.Cells(row1, "B").Select
  57. Exit Sub
  58. End If
  59. If keyc <> "" And sh1.Cells(row1, col1).Value <> "" Then
  60. col2 = dicH(keyc)
  61. row2 = dicD(keyr)
  62. sh2.Cells(row2, col2).Value = sh2.Cells(row2, col2).Value + sh1.Cells(row1, col1).Value
  63. End If
  64. Next
  65. Next
  66. MsgBox ("完了")
  67. End Sub
  68.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty