0

In column 'M' i have hundreds of rows with multiple duplicates. I only want one record to show per duplicate when i run my macro. Below is my code and it deletes all records apart from one.

Sub DeleteRows()

With ActiveSheet
    Set Rng = Range("M5:M").End(xlDown)
    Rng.RemoveDuplicates Columns:=1, Header:=xlYes
End With

End Sub

It starts from M5 as this is where the data is initially. Why would it only be showing one record?

3
  • 1
    Set Rng = Range("M5").End(xlDown) is one cell. Commented Oct 29, 2019 at 15:45
  • Changed it ("M5:M") but still nothing :/ Commented Oct 29, 2019 at 15:50
  • 1
    Note: You should avoid ActiveSheet and instead define which sheet you are referencing through it's CodeName. Then input a dot (.) in front of all references to be explicit, e.g: .Range(... Commented Oct 29, 2019 at 17:09

1 Answer 1

1

Your original attempt, Range("M5").End(xlDown), is just one cell.

Your new attempt, Range("M5:M").End(xlDown), is closer but not a valid Range reference.

Try the following:

Set Rng = Range("M5:M" & Cells(Rows.Count, "M").End(xlUp).Row)

EDIT:

If you're dealing with an entire range, you need to specify the Columns argument of Range.RemoveDuplicates, something like this:

Sub RemoveDupes()
    Dim lastRow As Long
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row

    Range("A5:V" & lastRow).RemoveDuplicates Columns:=Array(13), Header:=xlYes ' column M = 13
End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

That removes all the duplicates in that columns which is good! But only column M has the duplicates removed i want the rows to be deleted aswell if that makes sense?
Then you need to specify the Columns argument of Range.RemoveDuplicates, and it shouldn't just be column M as the range reference. What column does your data start in and what column does it end in?
Debug.Print lastRow, is it what you want? Can't repro the error.
I have merged cells above A5 to V5 which is causing the error. ' All merged cells need to be the same size'
I removed them and still not working, this is frustrating

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.