0

I'm trying to RemoveDuplicates for each column EX) removeduplicates for A1:A100, columns=1 / B1:B10-, colums=2......so on

and the boldes codes don't work please help me out

Sub DeleteDulpicates()

Dim i As Integer
Dim X As Integer
Dim Endrow As Long
Dim Endcolumn As Long
Dim sht As Worksheet

Set sht = ActiveSheet

Endrow = Cells.SpecialCells(xlCellTypeLastCell).Row
Endcolumn = sht.Cells.SpecialCells(xlCellTypeLastCell).Column


For i = 1 To Endcolumn
    
***Range(sht.Cells(i, 1), sht.Cells(i, 15000)).RemoveDuplicates Columns:=i, Header:=xlNo***
Next
    
End Sub
2
  • From documentation, it looks like you need an array of column indexes so Columns:=Array(i) may fix the error. (also Endrow doesn't qualify the worksheet (i.e. sht. is missing. learn.microsoft.com/en-us/office/vba/api/… Commented Oct 7, 2021 at 9:05
  • 1
    @Tragamor: For one column a number is fine. For multiple columns, it has to be a 1. variant 2. zero-based array, and 3. it has to be evaluated if you don't want to hard-code it (e.g. Array(1, 2)). Check out this example. Commented Oct 7, 2021 at 9:45

1 Answer 1

1

You have row and column the wrong way round in Cells, and if you are removing duplicates from individual columns the Columns argument should be 1.

Option Explicit

Sub DeleteDulpicates()
Dim sht As Worksheet
Dim i As Long
Dim Endcolumn As Long
Dim Endrow As Long

    Set sht = ActiveSheet

    Endrow = Cells.SpecialCells(xlCellTypeLastCell).Row
    Endcolumn = sht.Cells.SpecialCells(xlCellTypeLastCell).Column

    For i = 1 To Endcolumn
        With sht
            .Range(.Cells(1, i), .Cells(Endrow, i)).RemoveDuplicates Columns:=1, Header:=xlNo
        End With
    Next i

End Sub

Sign up to request clarification or add additional context in comments.

1 Comment

thank you you saved my life.....my savior..........my angel.....i love u

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.