if/then/else removing duplicates

ajedmonds

New Member
Joined
Oct 5, 2015
Messages
13
Hi

I want a macro to remove duplicates based on the set of criteria I've articulated below, is this possible?

If
Column C value is greater than Column E value
then
Select rows with identical Column C value and remove duplicate rows based on values in Column A, B, C, D
else
Select rows with identical Column E value and remove duplicate rows based on values in Column A, B, D, E


Thanks for your time!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this.
For cols A to E, no column headers.
Could be either col C > col E or vice versa. Easy modified whichever. Check for yourself.
Try on test data and provide feedback if need be.
Code:
Sub dups_wif_conditions()

Dim Dic As Object
Dim s(), a As Range, arr, u, x, Kx, w1 As String, w2 As String
Dim i As Long, c As Long, cls As Long, rws As Long

arr = Array(1, 2, 3, 4, 5)  'array to specify columns for duplicates

Set Dic = CreateObject("scripting.dictionary")
With Range("A1").CurrentRegion
    Set a = .Resize(, .Columns.Count + 1)
End With
rws = a.Rows.Count
cls = a.Columns.Count

ReDim s(1 To rws, 1 To 1)

With Application
    .ScreenUpdating = False
    .Calculation = xlManual
    For i = 1 To UBound(arr) + 1
        Cells(1, i).Resize(rws).Name = "n." & i
    Next i
    
    w1 = "n.1&char(2)&n.2&char(2)&n.4&char(2)&"
    w2 = "if(n.3 < n.5, n.3, n.5)"
    x = Evaluate(w1 & w2)
    
    For i = 1 To rws
        If Not Dic(x(i, 1)) Then Dic(x(i, 1)) = True _
            Else s(i, 1) = 1: c = c + 1
    Next i

    If c > 0 Then
        a(cls).Resize(rws) = s
        a.Sort a.Columns(cls)
        a.Resize(c).Delete xlUp
    End If
    
    For Each Kx In ActiveWorkbook.Names
        Kx.Delete
    Next Kx
    
    .Calculation = xlAutomatic
    .ScreenUpdating = True
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

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