Delete Duplicates in a Column, For all Columns

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
970
Office Version
  1. 2021
Platform
  1. Windows
Hello All
I have this:
VBA Code:
Option Explicit
Dim sht As Worksheet

Sub RemoveDuplicate()

    For Each sht In ActiveWorkbook.Worksheets
        If sht.Visible And sht.Name = "Matrix" Then
            sht.Activate
            ActiveSheet.Range("CN:CN").RemoveDuplicates Columns:=1, Header:=xlYes
        End If
    Next sht
    
End Sub

This is working to find and remove duplicates in Column CN. However, I don't know how to loop thru all columns (beginning with column E) to the end of columns where no data exist anymore, to find duplicates in each individual column.
Currently, if I want to find duplicates in Column L, then I change the CN to L and run the macro again...and so on.
Thanks for the help
 
Try this
VBA Code:
Sub RemoveDuplicate()
  Dim i As Long, sh As Worksheet
  Set sh = Sheets("Matrix")
  For i = 1 To sh.Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
    sh.Range(sh.Cells(5, i), sh.Cells(Rows.Count, i)).RemoveDuplicates Columns:=1, Header:=xlYes
  Next
End Sub
OK...I added a row, so now I want to have the code start looking in row 6. So I changed sh.Range(sh.Cells(5, i) to sh.Range(sh.Cells(6, i)...and it works. I now know what it is doing (sh.Cells(6,i) means the code begins in cell A6.
If I were to want the code to begin in say E6, I would change i=6, and keep sh.Range(sh.Cells(6, i), the same. Which I believe would make the code start looking for duplicates in cell E6.

Also...I almost forgot, I changed Header:=xl Yes to No

I believe everything is working correctly, because I now know where the code begins to look for duplicates.
thanks for all the help
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,892
Messages
6,127,610
Members
449,389
Latest member
ChessManNaill

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