Vba Macro for remove duplicate values and sum other column

Ghoghnuse

New Member
Joined
May 4, 2017
Messages
12
Hi

I have a table like this :

A
B
C
D
E
F
G
16
CODE
AMOUNT
17
202
100
18
203
200
19
202
200
20
205
300
21
203
500
22
208
150
23
203
100
24
205
500

<tbody>
</tbody>

















I Want a macro to remove Duplicate Codes and calculate sum of each codes and copy results to new sheet .

Like This :

Code
Sum of Amounts
202
300
203
800
205
800
208
150

<tbody>
</tbody>









I know that i could use Pivot Table but i want to use macro .

Thanks in Advance .
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
How about
Code:
Sub RemoveDupe_sum()


    Dim Cl As Range
    Dim Cnt As Long
    
    With CreateObject("scripting.dictionary")
        For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
            If Not .exists(Cl.Value) Then
                .Add Cl.Value, Cl.Offset(, 6).Value
            Else
                .Item(Cl.Value) = .Item(Cl.Value) + Cl.Offset(, 6).Value
            End If
        Next Cl
        Sheets("Sheet1").Range("A1").Resize(.Count).Value = Application.Transpose(.Keys)
        Sheets("Sheet1").Range("B1").Resize(.Count).Value = Application.Transpose(.items)
        End With

End Sub
 
Upvote 0
Thanks a lot Fluff ! Work like a charm ...

But how could i copy result to first blank cell end of column A in sheet1 ?
 
Last edited:
Upvote 0
Use
Code:
Sub RemoveDupe_sum()


    Dim Cl As Range
    Dim Cnt As Long
    
    With CreateObject("scripting.dictionary")
        For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
            If Not .exists(Cl.Value) Then
                .Add Cl.Value, Cl.Offset(, 6).Value
            Else
                .Item(Cl.Value) = .Item(Cl.Value) + Cl.Offset(, 6).Value
            End If
        Next Cl
        
        Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(.Count).Value = Application.Transpose(.Keys)
        Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Offset(1).Resize(.Count).Value = Application.Transpose(.items)
        End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,559
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top