Duplicate Combine & Add Macro

Stresbringer

Board Regular
Joined
Mar 16, 2006
Messages
84
Hiya All,

I have done some searching and nothing i have come accross quite decribes what i need,.
i hope someone can help me to make some sort of formula or macro which will find duplicate rows and add them together and combine them.Its kind of hard to explain but i will try to show you in my sample data below

ID No Name C D E F
1489 Mike 10 12 100 7
1489 Mike 150 3 100 4
1489 Mike 35 17 100 3

7457 Joe 11 50 200 4
7457 Joe 4 1 200 5

1359 Sue 20 90 150 25
1359 Sue 30 4 150 10
1359 Sue 5 6 150 7

I would like a macro or a formula which will Find the duplicates in a sheet and Combine and add the values together for C, D and F ID Name and E should remain the same.

From the data above result i want to gets is:

ID No Name C D E F

1489 Mike 195 32 100 14

7457 Joe 15 51 200 9

1359 Sue 55 100 150 42

as can see C D and F have been added together but the id and name remain the same, there can be upto 5 duplicated per person so i need to be able to allow for this.

i hope this makes sence

A macro of somesort would save endless hours of work and would cut out much human error, i am new to Vba but i hear you can do pretty much anything with it and i hope someone can tell me if this is possible or not.

Thanks in advance.
 
Hiya,


I have tried the new code but i cant seem to get it to work,
i keep getting a subscrupt out of range error, could someone tell me what im doing wrong ?

I can post some proper sample data with the desired results if it would be helpful. However, i feel its nearly right as the first code worked good except i had only antisipated 6 coloumns instead of 8.

Regards

Stres
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Please make yourself clear, confusing...

1) What error are you getting? Compile error ? RunTime Error? Which line?

2) how many columns do you actually have? 6 or 8?
 
Upvote 0
Hiyas,

thanks for you swift reply, heres is the cose as i have it
Code:
Sub test()
''' Original code was by jindon MVP I modified it for this post.
'' Thanks to jindon '''
Dim dic As Object, ws As Worksheet, z As String, a, result()
Set dic = CreateObject("scripting.dictionary")
dic.comparemode = vbTextCompare
For Each ws In Worksheets
    If ws.Name<> "Summary" Then
        a = ws.Range("a1", ws.Range("a" & Rows.Count).End(xlUp)).Resize(, 8).Value
        For i = 1 To UBound(a, 2)
            z = a(i, 1) & ":" & a(i, 2)
            If Not dic.exists(z) Then
                n = n + 1
                ReDim Preserve result(1 To 8, 1 To n)
                For ii = 1 To UBound(a, 2)
                    result(ii, n) = a(i, ii)
                Next ii
                dic.Add z, n
            Else
                For ii = 3 To 8
                    If ii<> 6 Then
                        result(ii, dic(z)) = result(ii, dic(z)) + a(i, ii)
                    End If
                Next ii
            End If
        Next i
    End If
Next
With Sheets("summary")
.Cells.Clear
.Range("a1").Resize(n, UBound(a, 2)) = Application.Transpose(result)
End With
Set dic = Nothing: Erase a, result
End Sub

sorry if i have been unclear, When i run the macro i get a runtime error 9 Subscript Out of range.

There are 8 columns altogether below is some sample data from the actual sheet need the macro for.
Book1
ABCDEFGHIJ
1PayrollnoFullNameBasicPayOvertimeTotalHoursRateTotalBonusTotalPay
221095Dave89.550.0014.906.010.0089.55
321095Dave96.16126.2137.006.010.00222.37
421095Dave90.150.0015.006.0115.71105.86
521004Jane1,062.640.00148.007.180.001,062.64
621063Bob395.400.0060.006.5964.05459.45
721063Bob392.110.0059.506.599.89402.00
8
9DesiredResult
1021095Dave275.86126.2166.96.0115.71417.78
1121004Jane1,062.640.00148.007.180.001,062.64
1221063Bob787.510.00119.56.5973.94861.45
13
14
15
Sheet1


I hope this explains it

Thanks for all your help
 
Upvote 0
If you have more than 5000 unique items as a result, it need to be altered
since transpose function only works with 5000 records,,,

Sorry, but I need to go off-line now.

I'll post the code tomorrow..

See ya
 
Upvote 0
I just want to make clear that

Current code loop through all the worksheets except "summary" sheet in the workbook

Are you sure about it?
 
Upvote 0
Hiya jindon,

Thanks for taking the time to help me with this.

I am a little unsure on what the mean here

just want to make clear that

Current code loop through all the worksheets except "summary" sheet in the workbook

Are you sure about it?

There will only ever be 2 sheets within my workbook, sheet1 with all the data on it and the summary sheet used for the code.

Sheet 1 has roughly 1500 rows and 8 columns, each month i plan to copy & paste the information i get over to sheet 1 and run the macro .


I hope i have answered the question

Thanks
 
Upvote 0
ok ,

Today u retried the code, and fir some reason it worked (sort of)

the result on the summary page only game me 8 results for some reason (including the header)

the code is below,
Code:
 Sub test()
''' Original code was by jindon MVP I modified it for this post.
'' Thanks to jindon '''
Dim dic As Object, ws As Worksheet, z As String, a, result()
Set dic = CreateObject("scripting.dictionary")
dic.comparemode = vbTextCompare
For Each ws In Worksheets
    If ws.Name <> "Summary" Then
        a = ws.Range("a1", ws.Range("a" & Rows.Count).End(xlUp)).Resize(, 8).Value
        For i = 1 To UBound(a, 2)
            z = a(i, 1) & ":" & a(i, 2)
            If Not dic.exists(z) Then
                n = n + 1
                ReDim Preserve result(1 To 8, 1 To n)
                For ii = 1 To UBound(a, 2)
                    result(ii, n) = a(i, ii)
                Next ii
                dic.Add z, n
            Else
                For ii = 3 To 8
                    If ii <> 6 Then
                        result(ii, dic(z)) = result(ii, dic(z)) + a(i, ii)
                    End If
                Next ii ''''' this was missing
            End If
        Next i
    End If
Next
With Sheets("summary")
.Cells.Clear
.Range("a1").Resize(n, UBound(a, 2)) = Application.Transpose(result)
End With
Set dic = Nothing: Erase a, result
End Sub

maybe i deleted someting i shouldnt have from the first code??
 
Upvote 0
OK
Can you delete
2 lines after dic.comparemode = vbTextCompare that is:
Code:
For Each ws In Worksheets
    If ws.Name <> "summary" Then
and put following one line in that spot:
Code:
With Sheets("Sheet1")
then change next line as
Code:
a = .Range("a1", .Range("a" & Rows.Count).End(xlUp)).Resize(,8).Value
Delete following 2 lines before With Sheets("summary") that is:
Code:
      End If
Next
and enter[End With[/code] in that position

let's see if any difference....
 
Upvote 0
ok have changed the code to the one below:


Code:
 Sub test()
''' Original code was by jindon MVP I modified it for this post.
'' Thanks to jindon '''
Dim dic As Object, ws As Worksheet, z As String, a, result()
Set dic = CreateObject("scripting.dictionary")
dic.comparemode = vbTextCompare
With Sheets("Sheet1")
        a = .Range("a1", .Range("a" & Rows.Count).End(xlUp)).Resize(, 8).Value
        For i = 1 To UBound(a, 2)
            z = a(i, 1) & ":" & a(i, 2)
            If Not dic.exists(z) Then
                n = n + 1
                ReDim Preserve result(1 To 8, 1 To n)
                For ii = 1 To UBound(a, 2)
                    result(ii, n) = a(i, ii)
                Next ii
                dic.Add z, n
            Else
                For ii = 3 To 8
                    If ii <> 6 Then
                        result(ii, dic(z)) = result(ii, dic(z)) + a(i, ii)
                    End If
                Next ii
            End If
        Next i
End With
With Sheets("summary")
.Cells.Clear
.Range("a1").Resize(n, UBound(a, 2)) = Application.Transpose(result)
End With
Set dic = Nothing: Erase a, result
End Sub

itv runs ok with no errors. However on the summary sheet i still only get 8 results (9 including the row header) =(
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,722
Members
449,465
Latest member
TAKLAM

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