find duplicates based on mult rows Hiker95 Code

mswainson

New Member
Joined
Aug 31, 2017
Messages
3
Hiker95,
I found a code you posted a few years ago (awesome code btw) but I was wondering if possibly you might could help me with it.
this is the original post. https://www.mrexcel.com/forum/excel...duplicate-records-then-delete-duplicates.html

What I would like for this to do is, look at Column D for Matches and then check for duplicate entries against those results in Col AB, BS, & BW check those rows for duplicate results and delete the dups and SUM the remaining in the first row. I can't delete the rows but I need to clear the extra results in those cells. If they are duplicated clear the cell if they are not add them to the sum.




D AB BS BW
1234 10 200 1000
1234 10 200 1000
1234 10 150 1000
1234 2 150 1000
2345 20 250 2000

Results should be

D AB BS BW
1234 12 350 1000
1234
1234
1234
2345 20 250 2000

any help would be much appreciated, Thanks.

This is how I have modified the code to work with my data so far. Im new to VBS so I'm just leaning and still have a really long ways to go.



Code:
 Sub ReorgDataSumCount()
 ' hiker95, 08/02/2012
 ' http://www.mrexcel.com/forum/showthread.php?650979-VBA-Code-to-Sum-Duplicates



 Dim r As Long, lr As Long, n As Long
 Dim s As Long, t As Long

 ' turn off screen updating
 Application.ScreenUpdating = False


 ' clear column FV - FY
 Columns("FV:FY").ClearContents


 ' put 'Deleted' in FV1
 '   and make it BOLD, and, centered horizontally
 With Range("FV1")
   .Value = "Deleted"
   .Font.Bold = True
   .HorizontalAlignment = xlCenter
 End With

 With Range("FW1")
   .Value = "Total F1 Cost"
   .Font.Bold = True
   .HorizontalAlignment = xlCenter
 End With

 With Range("FX1")
   .Value = "Total F2 Cost"
   .Font.Bold = True
   .HorizontalAlignment = xlCenter
 End With

 With Range("FY1")
   .Value = "LU Count"
   .Font.Bold = True
   .HorizontalAlignment = xlCenter
 End With

 Sheets("WPC04").Cells(2, "FW").Formula = "=BT2"
 Sheets("WPC04").Cells(2, "FX").Formula = "=BW2"
 Sheets("WPC04").Cells(2, "FY").Formula = "=AB2"


 ' find the lastrow in column D
 lr = Cells(Rows.Count, "D").End(xlUp).Row

     Range("FW2:FY2").Select
     Selection.AutoFill Destination:=Range("FW2:FY" & lr)
     Range("FW2:FY" & lr).Select

 ' sort the raw data by column D, beginning in B2,
 '   the 1 stands for ascending
 Range("A2:FY" & lr).Sort key1:=Range("D2"), order1:=1


 ' lets loop thru the raw data
 '    beginning in row 2
 For r = 2 To lr


   ' count how name '41234's are in column 4 = D
   n = Application.CountIf(Columns(4), Cells(r, 4).Value)


   ' if there is only one of them
   If n = 1 Then


     ' put a 0 in column FV, row r
     Range("FV" & r) = 0

   
   ' is there is more than one
   ElseIf n > 1 Then


     ' put n - 1 in column FV, row r
     Range("FV" & r) = n - 1
     
     
     
     ' the first row for each group
     '   in each respective column
     '   put the value of the sum of that range for that column
     '   in the first row for each column
     
     Range("BT" & r).Value = Evaluate("=Sum(BT" & r & ":BT" & r + n - 1 & ")")
     Range("BW" & r).Value = Evaluate("=Sum(BW" & r & ":BW" & r + n - 1 & ")")
     Range("AB" & r).Value = Evaluate("=Sum(AB" & r & ":AB" & r + n - 1 & ")")
       
         
     
     ' then blank out column from the second row of the group
     '   to the last row of that group
     Range("AB" & r + 1 & ":AB" & r + n - 1) = ""
     Range("BT" & r + 1 & ":BT" & r + n - 1) = ""
     Range("BW" & r + 1 & ":BW" & r + n - 1) = ""
   End If
   
   
   ' loop to the next group of items in column B
   r = r + n - 1
 Next r


 ' adjust the column widths for column A thru K
 Columns("A:FV").AutoFit


 ' turn screen updatting back on
 Application.ScreenUpdating = True




 MsgBox "Done!"
 End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Re: find duplicates based on mult rows Hiker95 Code help

Try this:-
Code:
[COLOR=navy]Sub[/COLOR] MG03Sep16
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, n [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Txt [COLOR=navy]As[/COLOR] [COLOR=navy]String,[/COLOR] nRng [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Dim[/COLOR] Dic [COLOR=navy]As[/COLOR] Object, Dic1 [COLOR=navy]As[/COLOR] Object, k [COLOR=navy]As[/COLOR] Variant
[COLOR=navy]Dim[/COLOR] Cm [COLOR=navy]As[/COLOR] Variant, Cols [COLOR=navy]As[/COLOR] Variant, temp [COLOR=navy]As[/COLOR] Double, DicA [COLOR=navy]As[/COLOR] Object
[COLOR=navy]
Set[/COLOR] Rng = Range("D2", Range("D" & Rows.Count).End(xlUp))
[COLOR=navy]Set[/COLOR] DicA = CreateObject("scripting.dictionary")
DicA.CompareMode = vbTextCompare
[COLOR=navy]
For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    [COLOR=navy]If[/COLOR] Not DicA.exists(Dn.Value) [COLOR=navy]Then[/COLOR]
        DicA.Add Dn.Value, Dn
    [COLOR=navy]Else[/COLOR]
         [COLOR=navy]Set[/COLOR] DicA(Dn.Value) = Union(DicA(Dn.Value), Dn)
    [COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR]
[COLOR=navy]
Set[/COLOR] Dic = CreateObject("Scripting.Dictionary")
    Dic.CompareMode = 1
    Cols = Array(24, 67, 71)
[COLOR=navy]
For[/COLOR] [COLOR=navy]Each[/COLOR] Cm [COLOR=navy]In[/COLOR] Cols
   [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
        [COLOR=navy]If[/COLOR] Not Dic.exists(Dn.Value) [COLOR=navy]Then[/COLOR]
             [COLOR=navy]Set[/COLOR] Dic(Dn.Value) = CreateObject("Scripting.Dictionary")
        [COLOR=navy]End[/COLOR] If
        [COLOR=navy]If[/COLOR] Not Dic(Dn.Value).exists(Dn.Offset(, Cm).Value) [COLOR=navy]Then[/COLOR]
                Dic(Dn.Value).Add (Dn.Offset(, Cm).Value), Dn.Offset(, Cm)
        [COLOR=navy]Else[/COLOR]
                [COLOR=navy]Set[/COLOR] Dic(Dn.Value).Item(Dn.Offset(, Cm).Value) = _
                Union(Dic(Dn.Value).Item(Dn.Offset(, Cm).Value), Dn.Offset(, Cm))
        [COLOR=navy]End[/COLOR] If
    [COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]Next[/COLOR] Cm

[COLOR=navy]Set[/COLOR] Dic1 = CreateObject("scripting.dictionary")
Dic1.CompareMode = vbTextCompare
Cols = Array(24, 67, 71)
    
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] k [COLOR=navy]In[/COLOR] Dic.keys
        [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] P [COLOR=navy]In[/COLOR] Dic(k)
           [COLOR=navy]If[/COLOR] Dic(k).Item(P).Offset(, Cm).Count > 1 [COLOR=navy]Then[/COLOR]
                temp = Dic(k).Item(P)(1).Offset(, Cm)
                Dic(k).Item(P).Offset(, Cm).ClearContents
                Dic(k).Item(P)(1).Offset(, Cm) = temp
            [COLOR=navy]End[/COLOR] If
        [COLOR=navy]Next[/COLOR] P
[COLOR=navy]Next[/COLOR] k
[COLOR=navy]
For[/COLOR] [COLOR=navy]Each[/COLOR] Cm [COLOR=navy]In[/COLOR] Cols
       [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] k [COLOR=navy]In[/COLOR] DicA.keys
           temp = Application.Sum(DicA(k).Offset(, Cm))
           DicA(k).Offset(, Cm).ClearContents
           DicA(k)(1).Offset(, Cm).Value = temp
        [COLOR=navy]Next[/COLOR] k
    [COLOR=navy]Next[/COLOR] Cm
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0
Re: find duplicates based on mult rows Hiker95 Code help

D AB BS BW
1234 10 200 1000
1234 10 200 1000

1234 10 150 1000
1234 2 150 1000

2345 20 250 2000

Results should be

D AB BS BW
1234 12 350 1000
1234
1234
1234
2345 20 250 2000
I can understand why the first two rows (red) would combine to this...

1234 10 200 1000

and why the third and fourth row (blue) would combine to this...

1234 12 150 1000

but I don't understand why the full combination combines to this...

1234 12 350 2000

and not this instead...

1234 22 350 2000

Can you elaborate on the rule governing this?
 
Last edited:
Upvote 0
Re: find duplicates based on mult rows Hiker95 Code help

The first column is the job number. the AB, BS, & BW columns are unit totals. If they match the job number but are duplicates i want to throw them out. if they are not duplicates i want to sum the totals based on the D column job numbers
so the first two rows completely match so i want to get rid of the totals in ab, bs, bw of the second row. the third row doesn't match in columns bs but does match in ab & bw. so add bs to the first row sum and throw out ab & bw.
the fourth row doesn't match AB so add the "2" to the sum of the first row but the 150 is a duplicate of the third row so i don't want to add it again. Hope this makes it clearer.
 
Upvote 0
Re: find duplicates based on mult rows Hiker95 Code help

Please note:-
When trying my code, the actual data is assumed to start on row 2.
Regrds Mick
 
Upvote 0
Re: find duplicates based on mult rows Hiker95 Code help

The first column is the job number. the AB, BS, & BW columns are unit totals. If they match the job number but are duplicates i want to throw them out. if they are not duplicates i want to sum the totals based on the D column job numbers
so the first two rows completely match so i want to get rid of the totals in ab, bs, bw of the second row. the third row doesn't match in columns bs but does match in ab & bw. so add bs to the first row sum and throw out ab & bw.
the fourth row doesn't match AB so add the "2" to the sum of the first row but the 150 is a duplicate of the third row so i don't want to add it again. Hope this makes it clearer.
I think what you said makes sense, sort of, IF the data is always in "sort" order shown. In other words, I would have a problem knowing what to do if the data looked like this...

1234 10 200 1000
1234 2 150 1000
1234 10 200 1000
1234 10 150 1000
2345 20 250 2000

or this...

1234 10 200 1000
1234 2 150 1000
1234 10 200 1000
1234 10 150 1500
2345 20 250 2000

So, can the data ever be arranged like the first or second one (note the quantity change in the last column)? If so, show me the rationale on combining them.
 
Upvote 0
Re: find duplicates based on mult rows Hiker95 Code help

Mick, I had to adjust the array to 68 from 67 but otherwise it appears to do exactly what I wanted it to do! You are awesome thank you very much. I think this will work just fine!!!
 
Upvote 0
Re: find duplicates based on mult rows Hiker95 Code help

You're very welcome
I'm glad it eventually worked for you.
 
Upvote 0
Re: find duplicates based on mult rows Hiker95 Code help

MickG,

Another gem for my archives - thank you very much. (y)
 
Upvote 0
Re: find duplicates based on mult rows Hiker95 Code help

You're very welcome.
Thank you, for the Interest !!
 
Upvote 0

Forum statistics

Threads
1,214,849
Messages
6,121,922
Members
449,056
Latest member
denissimo

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