Remove Duplicates in Keeping MAX Numerical/Alphanumeric Value from the Deleted Row's Duplicate

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
In using Excell 2013 ->

Here is my Data:

Column 1 | C2 | C3 | C4 | C5 | C6 | C7 |C8| C9 |C10|C11|C12|C13|C14|C15|C16|C17|C18|C19| C20|

XY 1559 | ABC | XY | 1559 | 1559 | ABC | 159 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11| 12 | 13 |

XY 1559 | | XY | 1559 | 1645 | ABC | 250 | | | | | | | | | | | | | |

XY 1559 | | XY | 1559 | 1559 | ABC | 159 | | | | | | | | | | | | | |


________________________________________________

What I would theoretically like; by way of VBA Method is to Delete Duplicates by way of the Common Qualifier in Column 1 in keeping the Maximum Numerical Values of its predecessor’s rows with the same like Qualifier.

So then the Data will look after Macro execution; Control + Shift + Whatever, to then look like this below, in following this example for a large data base:</SPAN>


Column 1| C2 | C3 | C4 | C5 | C6 | C7 |C8|C9|C10|C11|C12|C13|C14|C15|C16|C17|C18|C19|C20|

XY 1559 | ABC | XY | 1559 | 1645 | ABC | 250 | 1 | 2| 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |



It seems like such a simple concept, but I'm getting no success with this one. Please Help!


:confused:
 
See if this does what you need

Code:
Sub bTest()
    Dim i As Long, j As Long, MaxCol5 As Long
    Dim vData As Variant, v As Variant, lin As Long
    Dim dict As Object
    
    Set dict = CreateObject("Scripting.Dictionary")
    dict.comparemode = vbTextCompare
    
    With Sheets("Sheet1").Range("C2:V573")
        vData = .Value
        
        For i = 1 To .Rows.Count
            If vData(i, 1) <> "" Then
                MaxCol5 = Evaluate("=MAX(IF(C2:C573=""" & vData(i, 1) & """,0+G2:G573))")
                vData(i, 1) = IIf(MaxCol5 - Val(vData(i, 5)) > 1200, Trim(vData(i, 1)), Trim(vData(i, 1)) & " ")
                If dict.Exists(vData(i, 1)) Then
                    For j = 2 To .Columns.Count
                        If .Cells(i, j) > dict.Item(vData(i, 1)).Cells(1, j) Then _
                            dict.Item(vData(i, 1)).Cells(1, j) = .Cells(i, j)
                    Next j
                Else
                    dict.Add vData(i, 1), .Rows(i).Cells
                End If
            End If
        Next i
                
        For Each v In dict.keys
            lin = lin + 1
            .Range("A" & lin).Resize(, .Columns.Count).Value = dict.Item(v).Cells.Value
        Next v
        
        .Rows(dict.Count + 1 & ":" & 573).ClearContents
    End With
End Sub

M.
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi M.

It looks like the trial runs that I ran are 100% accurate and successful! I'll have to KIT an let you know my progress.

Your Codes Are Second To None!

Keep on writing the good fight!

All the best!

R/
-P
 
Upvote 0
Hi M,

Do you think you can add an extra function to this code, at the end, to then finally sort by ascending value, from smallest to largest according to the values of Column 5(G) expanded selection to all of the columns present, or will I better off just recording a macro for that?

Thanks,

-P
 
Upvote 0
Hi Marco,

How's it going? Your code works beautifully.

I just would like to know how I would extend the range to be C2:V604?

I tried it myself but I am unsuccessful in all my attempts.

I'd really appreciate, if you could show me the light.

Many thanks in advance.

R/
-P
 
Upvote 0
Hi Marcelo,

What I wanted to ask you, is if you can modify this code to include the following rule:

Where, if and only if, there are values specifically posted for Column(s) G & I; between Rows 201-401; they are to over ride the MAX VALUE rule and be the VALUES taken; regardless if the VALUES are LESS or MORE then all the other Rows (being then 1-200;402-573).

I know, its be awhile, and your help would really be very much appreciated.

Thanks Marcelo, in advance.

R/
Pinaceous
 
Upvote 0
Code:
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+D
'


      Dim i As Long, j As Long, MaxCol5 As Long
    Dim vData As Variant, v As Variant, lin As Long
    Dim dict As Object
    
    Set dict = CreateObject("Scripting.Dictionary")
    dict.comparemode = vbTextCompare
    
    With Sheets("Sheet1").Range("C2:Z573")
        vData = .Value
        
        For i = 1 To .Rows.Count
            If vData(i, 1) <> "" Then
                MaxCol5 = Evaluate("=MAX(IF(Sheet1!C2:C573=""" & vData(i, 1) & _
                    """,IF(ISNUMBER(0+Sheet1!G2:G573),0+Sheet1!G2:G573)))")
                vData(i, 1) = IIf(MaxCol5 - Val(vData(i, 5)) > 1200, Trim(vData(i, 1)), Trim(vData(i, 1)) & " ")
                If dict.Exists(vData(i, 1)) Then
                    For j = 2 To .Columns.Count
                        If .Cells(i, j) > dict.Item(vData(i, 1)).Cells(1, j) Then _
                            dict.Item(vData(i, 1)).Cells(1, j) = .Cells(i, j)
                    Next j
                Else
                    dict.Add vData(i, 1), .Rows(i).Cells
                End If
            End If
        Next i
                
        For Each v In dict.keys
            lin = lin + 1
            .Range("A" & lin).Resize(, .Columns.Count).Value = dict.Item(v).Cells.Value
        Next v
        
        .Rows(dict.Count + 1 & ":" & 573).ClearContents
        
        'Sort by 5th column of the range
        .Sort Key1:=.Range("E1"), Order1:=xlAscending, Header:=xlNo
    End With
    
 
 End Sub
 
Upvote 0
Hi Marcelo,

I've posted several threads, to see if I someone can come close to scripting &/or understanding what I want and I'm getting nowhere.

I'm posting this anyway, to see maybe if I can regain your invaluable input.


There is one variation on this rule pertaining to the common qualifier found, I’d like for you to write into your code of which I’d like to explain by the following tables:



Representing Data From:<o:p></o:p>
Column C<o:p></o:p>
D<o:p></o:p>
E<o:p></o:p>
F<o:p></o:p>
G<o:p></o:p>
H<o:p></o:p>
I<o:p></o:p>
J<o:p></o:p>
K<o:p></o:p>
L<o:p></o:p>
M<o:p></o:p>
N<o:p></o:p>
O<o:p></o:p>
P<o:p></o:p>
Q<o:p></o:p>
R<o:p></o:p>
S<o:p></o:p>
T<o:p></o:p>
U<o:p></o:p>
V<o:p></o:p>
Rows.2-172<o:p></o:p>
XY155<o:p></o:p>
ABC<o:p></o:p>
XY<o:p></o:p>
155<o:p></o:p>
1559<o:p></o:p>
ABC<o:p></o:p>
259<o:p></o:p>
1<o:p></o:p>
2<o:p></o:p>
3<o:p></o:p>
4<o:p></o:p>
5<o:p></o:p>
6<o:p></o:p>
7<o:p></o:p>
8<o:p></o:p>
9<o:p></o:p>
10<o:p></o:p>
11<o:p></o:p>
12<o:p></o:p>
13<o:p></o:p>
Rows.2-172<o:p></o:p>
AB199<o:p></o:p>
CSE<o:p></o:p>
AB<o:p></o:p>
199<o:p></o:p>
1845<o:p></o:p>
CSE<o:p></o:p>
250<o:p></o:p>
14<o:p></o:p>
15<o:p></o:p>
16<o:p></o:p>
17<o:p></o:p>
18<o:p></o:p>
19<o:p></o:p>
20<o:p></o:p>
21<o:p></o:p>
22<o:p></o:p>
23<o:p></o:p>
24<o:p></o:p>
25<o:p></o:p>
26<o:p></o:p>
Rows.173-372<o:p></o:p>
XY155<o:p></o:p>
XY<o:p></o:p>
155<o:p></o:p>
1459<o:p></o:p>
ABC<o:p></o:p>
159<o:p></o:p>
Rows.173-372<o:p></o:p>
AB199<o:p></o:p>
AB<o:p></o:p>
199<o:p></o:p>
1945<o:p></o:p>
CSE<o:p></o:p>
350<o:p></o:p>
Rows.373-573<o:p></o:p>
XY155<o:p></o:p>
XY<o:p></o:p>
155<o:p></o:p>
1559<o:p></o:p>
ABC<o:p></o:p>
259<o:p></o:p>
Rows.373-573<o:p></o:p>
AB199<o:p></o:p>
AB<o:p></o:p>
199<o:p></o:p>
1845<o:p></o:p>
CSE<o:p></o:p>
250<o:p></o:p>

<tbody>
</tbody>

<tbody>
</tbody>


So, if the 'Common Qualifier' in Column C is also found within the Range of Rows.173-373 it’s values for Columns G & I only are to be taken regardless of any of the other values listed with the same 'Qualifier'.


So then the Data Result after macro execution; will result in the following table:



Representing:<o:p></o:p>
Column C<o:p></o:p>
D<o:p></o:p>
E<o:p></o:p>
F<o:p></o:p>
G<o:p></o:p>
H<o:p></o:p>
I<o:p></o:p>
J<o:p></o:p>
K<o:p></o:p>
L<o:p></o:p>
M<o:p></o:p>
N<o:p></o:p>
O<o:p></o:p>
P<o:p></o:p>
Q<o:p></o:p>
R<o:p></o:p>
S<o:p></o:p>
T<o:p></o:p>
U<o:p></o:p>
V<o:p></o:p>
Result of Code: now populated in Rows.2-172<o:p></o:p>
XY155<o:p></o:p>
ABC<o:p></o:p>
XY<o:p></o:p>
159<o:p></o:p>
1459<o:p></o:p>
ABC<o:p></o:p>
159<o:p></o:p>
1<o:p></o:p>
2<o:p></o:p>
3<o:p></o:p>
4<o:p></o:p>
5<o:p></o:p>
6<o:p></o:p>
7<o:p></o:p>
8<o:p></o:p>
9<o:p></o:p>
10<o:p></o:p>
11<o:p></o:p>
12<o:p></o:p>
13<o:p></o:p>
Result of Code: now populated in Rows.2-172<o:p></o:p>
AB199<o:p></o:p>
CSE<o:p></o:p>
AB<o:p></o:p>
199<o:p></o:p>
1945<o:p></o:p>
ABC<o:p></o:p>
350<o:p></o:p>
14<o:p></o:p>
15<o:p></o:p>
16<o:p></o:p>
17<o:p></o:p>
18<o:p></o:p>
19<o:p></o:p>
20<o:p></o:p>
21<o:p></o:p>
22<o:p></o:p>
23<o:p></o:p>
24<o:p></o:p>
25<o:p></o:p>
26<o:p></o:p>

<tbody>
</tbody>

<tbody>
</tbody>

Where you can see that Columns G & I values are displayed as a result because they were found in Rows.173-373.

Likewise, if there is no qualifier present in Rows.173-372;



Representing Data From:<o:p></o:p>
Column C<o:p></o:p>
D<o:p></o:p>
E<o:p></o:p>
F<o:p></o:p>
G<o:p></o:p>
H<o:p></o:p>
I<o:p></o:p>
J<o:p></o:p>
K<o:p></o:p>
L<o:p></o:p>
M<o:p></o:p>
N<o:p></o:p>
O<o:p></o:p>
P<o:p></o:p>
Q<o:p></o:p>
R<o:p></o:p>
S<o:p></o:p>
T<o:p></o:p>
U<o:p></o:p>
V<o:p></o:p>
Rows.2-172<o:p></o:p>
XY155<o:p></o:p>
ABC<o:p></o:p>
XY<o:p></o:p>
155<o:p></o:p>
1559<o:p></o:p>
ABC<o:p></o:p>
259<o:p></o:p>
1<o:p></o:p>
2<o:p></o:p>
3<o:p></o:p>
4<o:p></o:p>
5<o:p></o:p>
6<o:p></o:p>
7<o:p></o:p>
8<o:p></o:p>
9<o:p></o:p>
10<o:p></o:p>
11<o:p></o:p>
12<o:p></o:p>
13<o:p></o:p>
Rows.2-172<o:p></o:p>
AB199<o:p></o:p>
CSE<o:p></o:p>
AB<o:p></o:p>
199<o:p></o:p>
1845<o:p></o:p>
CSE<o:p></o:p>
250<o:p></o:p>
14<o:p></o:p>
15<o:p></o:p>
16<o:p></o:p>
17<o:p></o:p>
18<o:p></o:p>
19<o:p></o:p>
20<o:p></o:p>
21<o:p></o:p>
22<o:p></o:p>
23<o:p></o:p>
24<o:p></o:p>
25<o:p></o:p>
26<o:p></o:p>
Rows.173-372<o:p></o:p>
XY155<o:p></o:p>
XY<o:p></o:p>
155<o:p></o:p>
1459<o:p></o:p>
ABC<o:p></o:p>
159<o:p></o:p>
Rows.173-372<o:p></o:p>
Rows.373-573<o:p></o:p>
XY155<o:p></o:p>
XY<o:p></o:p>
155<o:p></o:p>
1559<o:p></o:p>
ABC<o:p></o:p>
259<o:p></o:p>
Rows.373-573<o:p></o:p>
AB199<o:p></o:p>
AB<o:p></o:p>
199<o:p></o:p>
1845<o:p></o:p>
CSE<o:p></o:p>
250<o:p></o:p>

<tbody>
</tbody>

<tbody>
</tbody>


Then the result would be;



Representing Data From:<o:p></o:p>
Column C<o:p></o:p>
D<o:p></o:p>
E<o:p></o:p>
F<o:p></o:p>
G<o:p></o:p>
H<o:p></o:p>
I<o:p></o:p>
J<o:p></o:p>
K<o:p></o:p>
L<o:p></o:p>
M<o:p></o:p>
N<o:p></o:p>
O<o:p></o:p>
P<o:p></o:p>
Q<o:p></o:p>
R<o:p></o:p>
S<o:p></o:p>
T<o:p></o:p>
U<o:p></o:p>
V<o:p></o:p>
Rows.2-172<o:p></o:p>
XY155<o:p></o:p>
ABC<o:p></o:p>
XY<o:p></o:p>
155<o:p></o:p>
1459<o:p></o:p>
ABC<o:p></o:p>
159<o:p></o:p>
1<o:p></o:p>
2<o:p></o:p>
3<o:p></o:p>
4<o:p></o:p>
5<o:p></o:p>
6<o:p></o:p>
7<o:p></o:p>
8<o:p></o:p>
9<o:p></o:p>
10<o:p></o:p>
11<o:p></o:p>
12<o:p></o:p>
13<o:p></o:p>
Rows.2-172<o:p></o:p>
AB199<o:p></o:p>
CSE<o:p></o:p>
AB<o:p></o:p>
199<o:p></o:p>
1845<o:p></o:p>
CSE<o:p></o:p>
250<o:p></o:p>
14<o:p></o:p>
15<o:p></o:p>
16<o:p></o:p>
17<o:p></o:p>
18<o:p></o:p>
19<o:p></o:p>
20<o:p></o:p>
21<o:p></o:p>
22<o:p></o:p>
23<o:p></o:p>
24<o:p></o:p>
25<o:p></o:p>
26<o:p></o:p>

<tbody>
</tbody>

<tbody>
</tbody>




This just adds to the work that you provided and it would be great if
you could finish it off.



Please Help!



R/

-Pin
 
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,876
Members
449,476
Latest member
pranjal9

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