Remove Duplicates by Row

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
946
Office Version
  1. 2016
Platform
  1. Windows
Hello All
I have the below code which is working good t remove duplicates by columns. I'm having issues trying to modify it to remove duplicates by Row, instead of columns.
I tried simply removing the "Column" part, and replacing with "Row"...but that was a catastrophe.
Is it possible to modify the below code to remove duplicates by row instead of columns?
Thank you

VBA Code:
Sub RemoveDuplicate()

Application.ScreenUpdating = True
Application.DisplayAlerts = False

  For i = 1 To Sheets("Matrix").Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
    Sheets("Matrix").Columns(i).RemoveDuplicates Columns:=1, Header:=xlYes
  Next


Application.ScreenUpdating = False
Application.DisplayAlerts = True
 
After running the last macros on my computer the following image is what I am left with.
Are you wanting Col E:G cleared of all data ? And, if so, If the entire columns are cleared is that going to cause an issue with data
on rows further down in those cols ?
 

Attachments

  • Del.jpg
    Del.jpg
    94.3 KB · Views: 7
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I tried the numbers in column/Row:
G:4H:4I:4
111
222
333
1
2
3
To get a general code that will work with other ranges, we would need some way to determine where the range starts and ends, but for that particular example, try this.

VBA Code:
Sub RemoveDupes()
  Dim d As Object
  Dim c As Range
  
  Set d = CreateObject("Scripting.Dictionary")
  For Each c In Range("G4:I9")
    If Not IsEmpty(c.Value) Then d(c.Value) = Empty
  Next c
  Range("K4").Resize(d.Count).Value = Application.Transpose(d.Keys)
End Sub

Here is my sample data and results.

Guzzlr.xlsm
GHIJK
41111
52222
63333
71
82
93
Sheet1
 
Upvote 0
After running the last macros on my computer the following image is what I am left with.
Are you wanting Col E:G cleared of all data ? And, if so, If the entire columns are cleared is that going to cause an issue with data
on rows further down in those cols ?
This is good, and will not hurt data further down, as this will be used as a template only. However, when I run it, cell A1 always had a duplicate for some reason. What is the code you used?
Thanks for the help
 
Upvote 0
This should do what you are seeking :

VBA Code:
Sub AmalgamateCols()

    Dim Col As Long
    Application.ScreenUpdating = False
  
    For Col = 1 To 39     '<--- adjust number of columns here
        Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(2644).Value = _
            Range(Cells(1, Col), Cells(2648, Col)).Value
    Next Col
    With Range("A1", Range("A" & Rows.Count).End(xlUp))
        .Replace 0, ""
        .Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo
    End With
    Application.ScreenUpdating = True
    DeleteDups
    
End Sub

Sub DeleteDups()
    
    Dim x               As Long
    Dim LastRow         As Long
    Dim Cells           As Range
    Application.ScreenUpdating = False
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        LastRow = LastRow
    End With
 
    For x = LastRow To 1 Step -1
        If Application.WorksheetFunction.CountIf(Range("A1:A" & x), Range("A" & x).Text) > 1 Then
            Range("A" & x).Delete
        End If
    Next x
    Application.ScreenUpdating = True
    
    Range("E:E,F:F,G:G").ClearContents
End Sub
 
Upvote 0
This should do what you are seeking :

VBA Code:
Sub AmalgamateCols()

    Dim Col As Long
    Application.ScreenUpdating = False
 
    For Col = 1 To 39     '<--- adjust number of columns here
        Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(2644).Value = _
            Range(Cells(1, Col), Cells(2648, Col)).Value
    Next Col
    With Range("A1", Range("A" & Rows.Count).End(xlUp))
        .Replace 0, ""
        .Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo
    End With
    Application.ScreenUpdating = True
    DeleteDups
   
End Sub

Sub DeleteDups()
   
    Dim x               As Long
    Dim LastRow         As Long
    Dim Cells           As Range
    Application.ScreenUpdating = False
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        LastRow = LastRow
    End With
 
    For x = LastRow To 1 Step -1
        If Application.WorksheetFunction.CountIf(Range("A1:A" & x), Range("A" & x).Text) > 1 Then
            Range("A" & x).Delete
        End If
    Next x
    Application.ScreenUpdating = True
   
    Range("E:E,F:F,G:G").ClearContents
End Sub

It Works!
Thank you
 
Upvote 0
It Works!
Thank you
Are you sure?

I just ran the codes with this sample data

Guzzlr.xlsm
ABC
119066
223334
334899
Sheet1 (3)


.. and this was the result

Guzzlr.xlsm
ABC
119066
223334
334899
49
533
634
748
866
999
Sheet1 (3)


You will note that the results include "9" which was not in the original data and omits 90 which was in the original data. (Ref the risky code line .Replace 0, "")

Other comments/questions
  • What is this line for LastRow = LastRow ?

  • Why does the sample code process 39 columns and remove 3 of them?

  • Doesn't the shorter (and faster) code in post #12 produce the correct results? (It does not sort because the original expected results in post 3 were not sorted, but could easily do that if required)
 
Upvote 0

Forum statistics

Threads
1,214,867
Messages
6,122,002
Members
449,059
Latest member
mtsheetz

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