How do I identify and delete rows with matching data mixed between multiple columns?

ChrisJP

New Member
Joined
Jan 8, 2018
Messages
6
Example:
Column 1Column 2
Row 1ABC123MNO456
Row 2ZZZ987YYY789
Row 3MNO456ABC123

<tbody>
</tbody>

How could I identify Row 1 and Row 3 as being matches/duplicates rows, when the columns may have the data swapped?

Ideally I would want the a VB script or way to delete the Row 3 automatically since it is a duplicate.

This may be basic, but I've been searching for a while now and can't quite find the answer. I appreciate any help you can provide.
 
This should do it

Code:
Sub t3()
Dim lr As Long, i As Long, fn As Range, fAdr As String
    With ActiveSheet
        lr = .Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
        For i = lr To 2 Step -1
            Set fn = .Range("B:B").Find(.Cells(i, 1).Value, , xlValues, xlWhole)
                If Not fn Is Nothing Then
                    fAdr = fn.Address
                    Do
                        If Trim(fn.Offset(, -1).Value) = Trim(.Cells(i, 2).Value) Then
                            .Rows(i).Delete
                        End If
                        Set fn = .Range("B:B").FindNext(fn)
                    Loop While fn.Address <> fAdr
                End If
        Next
    End With
End Sub
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
how about a formula, like
Code:
<b8,a8&"|"&b8,b8&"|"&a8)[ code]<b2,a2&"|"&b2,b2&"|"&a2)
= IF (A8 is less than B8, A8&"|"&B8, B8&"|"&A8)
to make a code of "earlier alphabetically" "|" "later alphabetically"

then in next column
Code:
=COUNTIF($C$1:C2,C2)>1

the records returning TRUE to then be deleted

would that be OK?</b8,a8&"|"&b8,b8&"|"&a8)[>
 
Last edited:
Upvote 0
Well, that formula didn't paste. A problem with posting less than symbols. The formula was
=if( a2 is less than b2, a2 & '|" & b2, b2 & "|" & a2 )
 
Last edited:
Upvote 0
This should do it

Code:
Sub t3()
Dim lr As Long, i As Long, fn As Range, fAdr As String
    With ActiveSheet
        lr = .Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
        For i = lr To 2 Step -1
            Set fn = .Range("B:B").Find(.Cells(i, 1).Value, , xlValues, xlWhole)
                If Not fn Is Nothing Then
                    fAdr = fn.Address
                    Do
                        If Trim(fn.Offset(, -1).Value) = Trim(.Cells(i, 2).Value) Then
                            .Rows(i).Delete
                        End If
                        Set fn = .Range("B:B").FindNext(fn)
                    Loop While fn.Address <> fAdr
                End If
        Next
    End With
End Sub


This worked perfectly! I really appreciate the help JLGWhiz. You've saved me days of manually finding duplicates in 2 columns and deleting them with this script. This will allow me to clean up a 100k records.
 
Upvote 0
This worked perfectly! I really appreciate the help JLGWhiz. You've saved me days of manually finding duplicates in 2 columns and deleting them with this script. This will allow me to clean up a 100k records.

Happy to help,
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,216,036
Messages
6,128,432
Members
449,452
Latest member
Chris87

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