Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. Option Explicit
  2.  
  3. Sub test()
  4. Dim a, i As Long, ii As Long, dic As Object
  5. Set dic = CreateObject("Scripting.Dictionary")
  6. dic.CompareMode = 1
  7. a = Sheets("sheet1").Cells(1).CurrentRegion.Value
  8. With CreateObject("Scripting.Dictionary")
  9. .CompareMode = 1
  10. For i = 2 To UBound(a, 1)
  11. If Not dic.Exists(a(i, 1)) Then dic(a(i, 2)) = dic.Count + 2
  12. If Not .Exists(a(i, 1)) Then
  13. Set .Item(a(i, 1)) = CreateObject("Scripting.Dictionary")
  14. .Item(a(i, 1)).CompareMode = 1
  15. End If
  16. .Item(a(i, 1))(a(i, 2)) = .Item(a(i, 1))(a(i, 2)) + a(i, 3)
  17. Next
  18. ReDim a(1 To .Count + 1, 1 To dic.Count + 1)
  19. a(1, 1) = Sheets("sheet1").[a1]
  20. For i = 0 To dic.Count - 1
  21. a(1, i + 2) = dic.Keys()(i)
  22. Next
  23. For i = 0 To .Count - 1
  24. a(i + 2, 1) = .Keys()(i)
  25. For ii = 2 To UBound(a, 2)
  26. a(i + 2, ii) = .items()(i)(a(1, ii)) + 0
  27. Next
  28. Next
  29. End With
  30. With Sheets("sheet3").Cells(1).Resize(UBound(a, 1), UBound(a, 2))
  31. .EntireColumn.ClearContents
  32. Sheets("sheet1").[a1].Copy .Rows(1)
  33. .Value = a: .Columns.AutoFit: .Parent.Activate
  34. End With
  35. End Sub
  36.  
  37. Option Explicit
  38.  
  39. Sub Sample()
  40. Dim ws As Worksheet, wsNew As Worksheet
  41. Dim tempArray As Variant, OutputAr() As Variant
  42. Dim officeCol As New Collection
  43. Dim productCol As New Collection
  44. Dim itm As Variant
  45. Dim lrow As Long, lcol As Long, totalsum As Long
  46. Dim i As Long, j As Long, k As Long
  47.  
  48. '~~> Input sheet
  49. Set ws = Sheet1
  50.  
  51. With ws
  52. '~~> Get Last Row and last column
  53. lrow = .Range("A" & .Rows.Count).End(xlUp).Row
  54. lcol = .Cells(1, Columns.Count).End(xlToLeft).Column
  55.  
  56. '~~> Store it in a temp array
  57. tempArray = .Range(.Cells(2, 1), .Cells(lrow, lcol)).Value
  58.  
  59. '~~> Create a unique collection using On error resume next
  60. On Error Resume Next
  61. For i = LBound(tempArray) To UBound(tempArray)
  62. officeCol.Add tempArray(i, 1), CStr(tempArray(i, 1))
  63. productCol.Add tempArray(i, 2), CStr(tempArray(i, 2))
  64. Next i
  65. On Error GoTo 0
  66. End With
  67.  
  68. '~~> Define you new array which will hold the desired output
  69. ReDim OutputAr(1 To officeCol.Count + 1, 1 To productCol.Count + 1)
  70.  
  71. '~~> Store the rows and columns in the array
  72. i = 2
  73. For Each itm In officeCol
  74. OutputAr(i, 1) = itm
  75. i = i + 1
  76. Next itm
  77. i = 2
  78. For Each itm In productCol
  79. OutputAr(1, i) = itm
  80. i = i + 1
  81. Next itm
  82.  
  83. '~~> Calculate sum by comparing the arrays
  84. For i = 2 To officeCol.Count + 1
  85. For j = 2 To productCol.Count + 1
  86. totalsum = 0
  87. For k = LBound(tempArray) To UBound(tempArray)
  88. If OutputAr(i, 1) = tempArray(k, 1) And _
  89. OutputAr(1, j) = tempArray(k, 2) Then
  90. totalsum = totalsum + tempArray(k, 3)
  91. End If
  92. Next k
  93.  
  94. OutputAr(i, j) = totalsum
  95. Next j
  96. Next i
  97.  
  98. '~~> Create a new sheet
  99. Set wsNew = ThisWorkbook.Sheets.Add
  100.  
  101. '~~> Outout the array
  102. wsNew.Range("A1").Resize(officeCol.Count + 1, productCol.Count + 1).Value = OutputAr
  103. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement