0

So, I am trying to delete any rows that have duplicate data in column A. I thought this was pretty elegant compared to some of the loops I saw people asking about. However, I get an error on the line rng.RemoveDuplicates Columns:=1, Header:=xlYes. The error is Run-time error 1004. Application defined or object defined error.

Ideally, we can get the error to go away while also removing duplicates rows. Any help would be appreciated! Thanks!

Private Sub RemoveDuplicate_Click()

  Dim LastRow As Long
  Dim rng As Range
  Dim ws As Worksheet
  Set ws = Worksheets("Scorecard")

  LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
  Set rng = ws.Range("A1:K" & LastRow)
  rng.RemoveDuplicates Columns:=1, Header:=xlYes
End Sub

Also, I tried this code as well and got the same error.

Private Sub RemoveDuplicate_Click()

 Dim LastRow As Long
 Dim rng As Range
 Worksheets("Scorecard").Activate
 LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
 Set rng = ActiveSheet.Range("A1:K" & LastRow)
 rng.RemoveDuplicates Columns:=1, Header:=xlYes

End Sub

1 Answer 1

1
rng.RemoveDuplicates Columns:=Array(1),  Header:=xlYes

https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-removeduplicates-method-excel

Columns: Array of indexes of the columns that contain the duplicate information.

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

5 Comments

I replaced my line of code with the one you said and still got the same error. I tried it with both of my examples above.The error is Run-time error 1004. Application defined or object defined error. Also occurring at the same spot.
What's the value of LastRow when it fails?
Your code works fine for me (even without the Array() edit)
I added a msgbox to print LastRow. It returns 138
There was a blank row. A2 was a blank row. I changed the code from set rng = ActiveSheet.Range("A1:K" & LastRow) to set rng = ActiveSheet.Range("A2:K" & LastRow) and it worked. Thanks Tim!

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.