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:
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Are those pipes "|" your version of a table? Click on advanced in the forum and insert a real table and change the table style to Full Grid.
AB
1datadata
2datadata

<tbody>
</tbody>
 
Upvote 0
Maybe this

Before macro

Sheet1

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
1
XY 1559​
ABC​
XY​
1559​
1559​
ABC​
159​
1​
2​
3​
4​
5​
6​
7​
8​
9​
10​
11​
12​
13​
2
XY 1559​
XY​
1559​
1645​
ABC​
250​
3
XY 1559​
XY​
1559​
1559​
ABC​
159​


After macro

Sheet1

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
1
XY 1559​
ABC​
XY​
1559​
1645​
ABC​
250​
1​
2​
3​
4​
5​
6​
7​
8​
9​
10​
11​
12​
13​
2


***Try the code below on a copy of your workbook***

Code:
Sub aTest()
    Dim lastRow As Long, lastCol As Long
    Dim rngData As Range, vData As Variant, arrResult() As Variant
    Dim lin As Long, col As Long, cellMax As Variant
    
    With Sheets("Sheet1") '<--- adjust sheet name
        lastRow = .Cells.Find(What:="*", SearchOrder:=xlRows, _
                    SearchDirection:=xlPrevious, LookIn:=xlValues).Row
                
        lastCol = .Cells.Find(What:="*", SearchOrder:=xlByColumns, _
                    SearchDirection:=xlPrevious, LookIn:=xlValues).Column
        
        Set rngData = .Range("A1", .Cells(lastRow, lastCol))
        
        vData = rngData.Value
        ReDim arrResult(1 To UBound(vData, 2))
        
        For col = 1 To UBound(vData, 2)
            cellMax = Empty
            For lin = 1 To UBound(vData, 1)
                If vData(lin, col) > cellMax Then cellMax = vData(lin, col)
            Next lin
            arrResult(col) = cellMax
        Next col
        
        rngData.ClearContents
        .Range("A1").Resize(, UBound(vData, 2)) = arrResult
    End With
    
End Sub

Hope this helps

M.
 
Upvote 0
Holy (*&^(*&(! It worked!

Now, how do I set the sheet# to be column specific upon exection? For example, between Column's C & V => specifically this area: $C$1:$V$574?

Your the best!
:)
 
Upvote 0
Column1
C2C3C4C5C6C7C8C9C10C11C12C13C14C15C16C17C18C19C20
XY 1559ABCXY15591559123456789101112131415
YZ 1444EFGYZ144417322468101214
AB 1111HIJAB1111132213579111315
XY 1559XY1559250
YZ 1444YZ1444222
AB 1111AB1111423
XY 1559XY15591559
YZ 1444YZ14441732
AB 1111AB11111322

<tbody>
</tbody>

Ok; So now in "Removing Duplicates", How can it keep the MAX Values in each respective row?

For example; How do I achieve this result after execution, below?

I'll have to submit it as a different thread, sorry.

Below
 
Upvote 0
Column1C2C3C4C5C6C7C8C9C10C11C12C13C14C15C16C17C18C19C20
XY 1559ABCXY15591645ABC25012345678910111213
YZ 1444EFGYZ14441834EFG222135791113
AB 1111HIJAB11111422HIJ42324681012

<tbody>
</tbody>


Where this is my result after the delete duplicate execution, it will only take the highest numerical value of each of its rows, respectively. Really, in C8 - C20; I just posted arbitrary place holders, just to prove the concept. Hope this gets my point across much clearer. Thanks All :cool:
 
Upvote 0
Maybe...

Before macro

Sheet1

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
1
XY 1559​
ABC​
XY​
1559​
1559​
ABC​
250​
1​
2​
3​
4​
5​
6​
7​
8​
9​
10​
11​
12​
13​
2
YZ 1444​
EFG​
YZ​
1444​
1732​
EFG​
222​
2​
4​
6​
8​
10​
12​
3
AB 1111​
HIJ​
AB​
1111​
1322​
HIJ​
423​
1​
3​
5​
7​
9​
11​
13​
4
XY 1559​
XY​
1559​
250​
5
YZ 1444​
YZ​
1444​
222​
6
AB 1111​
AB​
1111​
423​
7
XY 1559​
XY​
1559​
1645​
8
YZ 1444​
YZ​
1444​
1834​
9
AB 1111​
AB​
1111​
1422​

After macro

Sheet1

A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
1
XY 1559​
ABC​
XY​
1559​
1645​
ABC​
250​
1​
2​
3​
4​
5​
6​
7​
8​
9​
10​
11​
12​
13​
2
YZ 1444​
EFG​
YZ​
1444​
1834​
EFG​
222​
2​
4​
6​
8​
10​
12​
3
AB 1111​
HIJ​
AB​
1111​
1422​
HIJ​
423​
1​
3​
5​
7​
9​
11​
13​
4

***Try the code below on a copy of your workbook***

Code:
Sub aTest()
    Dim lastRow As Long, lastCol As Long, i As Long, j As Long
    Dim rngData As Range, vData As Variant, v As Variant, lin As Long
    Dim dict As Object
    
    Set dict = CreateObject("Scripting.Dictionary")
    dict.comparemode = vbTextCompare
    
    With Sheets("Sheet1")
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        Set rngData = .Range("A1", .Cells(lastRow, lastCol))
        vData = rngData.Value
        
        For i = 1 To UBound(vData, 1)
            If dict.Exists(vData(i, 1)) Then
                For j = 2 To lastCol
                    If .Cells(i, j) > dict.Item(vData(i, 1)).Cells(1, j - 1) Then _
                        dict.Item(vData(i, 1)).Cells(1, j - 1) = .Cells(i, j)
                Next j
            Else
                dict.Add vData(i, 1), .Range(.Cells(i, 2), .Cells(i, lastCol)).Cells
            End If
        Next i
        
        For Each v In dict.keys
            lin = lin + 1
            Sheets("Sheet2").Range("A" & lin) = v
            Sheets("Sheet2").Range("B" & lin).Resize(, dict.Item(v).Columns.Count).Value = dict.Item(v).Cells.Value
        Next v
        
        rngData.Rows(dict.Count + 1 & ":" & lastRow).ClearContents
    
    End With
        
End Sub

M.
 
Upvote 0
Okay M. This is Great. Just, How do I restrict this Macro to only execute between Excel's Column's C to Column V => in area of $C$1:$V$574?


:confused:
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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