kaneda0149
Board Regular
- Joined
- Aug 4, 2009
- Messages
- 74
Hi,
I found this fabulous code that sum the column and delete duplicate rows. It also put the updated data in a new sheet. This code is based on 2 columns; A and B. Column A being the duplicate rows and column B the values.
Can someone help in showing me how to sum column C of values as well. So the data would have 3 columns that looks like this:
A B C
Model type1 type2
1234 2 1
4321 1 4
1234 2 3
4321 1 1
A B C
Model type1 type2
1234 4 4
4321 2 5
Here's the original code that works on 2 columns (which I would love to maintain if possible). All the help would be greatly appreciated. Thanks!
I found this fabulous code that sum the column and delete duplicate rows. It also put the updated data in a new sheet. This code is based on 2 columns; A and B. Column A being the duplicate rows and column B the values.
Can someone help in showing me how to sum column C of values as well. So the data would have 3 columns that looks like this:
A B C
Model type1 type2
1234 2 1
4321 1 4
1234 2 3
4321 1 1
A B C
Model type1 type2
1234 4 4
4321 2 5
Here's the original code that works on 2 columns (which I would love to maintain if possible). All the help would be greatly appreciated. Thanks!
Code:
Sub CreatePESummary()
Dim Cell As Range
Dim Data() As Variant
Dim DSO As Object
Dim Key As Variant
Dim Keys As Variant
Dim I As Long
Dim Item As Variant
Dim Items As Variant
Dim Rng As Range
Dim RngEnd As Range
Dim SumWks As Worksheet
Dim Wks As Worksheet
On Error Resume Next
Set SumWks = Worksheets("Summary Report")
If Err = 9 Then
Err.Clear
Worksheets.Add.Name = "Summary Report"
Set SumWks = ActiveSheet
Cells(1, "A") = "Investment"
Cells(1, "B") = "Total Amount"
Rows(1).Font.Bold = True
Columns("A:B").AutoFit
End If
On Error GoTo 0
Set DSO = CreateObject("Scripting.Dictionary")
DSO.CompareMode = vbTextCompare
For Each Wks In Worksheets
If Wks.Name <> SumWks.Name Then
Set Rng = Wks.Range("A1")
Set RngEnd = Rng.Cells(Rows.Count, Rng.Column).End(xlUp)
Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
For Each Cell In Rng
Key = Trim(Cell.Value)
Item = Cell.Offset(0, 1).Value
If Key <> "" Then
If Not DSO.Exists(Key) Then
DSO.Add Key, Item
Else
DSO(Key) = DSO(Key) + Item
End If
End If
Next Cell
End If
Next Wks
With SumWks
.UsedRange.Offset(1, 0).ClearContents
Keys = DSO.Keys
Items = DSO.Items
For I = 0 To DSO.Count - 1
.Cells(I + 2, "A") = Keys(I)
.Cells(I + 2, "B") = Items(I)
Next I
.UsedRange.Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _
Header:=xlYes, Orientation:=xlSortColumns
End With
Set DSO = Nothing
End Sub