Macro to remove Duploicates in Col A on sheet BR1 NV Units

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below to remove duplicates in Col A on sheet BR1 unit sales


I get a run time error -Application defined or object defined error

It would be appreciated if someone could kindly amend my code

Code:
 Sub RemoveDuplicates()
  Sheets("BR1 NV Units").Range("A1").CurrentRegion.RemoveDuplicates 9, 1
  End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hello Howard,

You should only need to change the 9 to 1.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Hi there

Have you tried the below...

If the first duplicate is not being removed, it's possible that the data range being used does not actually include the entire column A. You may want to adjust the range to include all the data in column A.

To ensure that only unique items are retained in column A, you can modify the code as follows:

VBA Code:
Sub RemoveDuplicates()
    Dim ws          As Worksheet
    Dim dataRange   As Range
    Dim lastRow     As Long
    
    Set ws = ThisWorkbook.Sheets("BR1 NV Units")
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Set dataRange = ws.Range("A1:A" & lastRow)
    
    On Error Resume Next
    dataRange.RemoveDuplicates Columns:=1, Header:=xlNo        'If your data range does include a header row, you can set the "Header" argument to "xlYes"
    If Err.Number <> 0 Then
        MsgBox "Error: " & Err.Description
    End If
    On Error GoTo 0
End Sub


In this modified code, the data range is set to cover all the data in column A, and the "Header" argument is set to "xlNo" to indicate that there is no header row.

If you still encounter issues with duplicates not being removed, it could be that there are some subtle differences in the data values (such as leading/trailing spaces or non-printable characters). You may need to clean up the data before removing duplicates to ensure that all values are considered identical.
 
Upvote 0
Solution
Hello Howard,

Your code amended as follows should work for you:

VBA Code:
Sub RemoveDuplicates()
         Sheets("BR1 NV Units").Range("A1").CurrentRegion.RemoveDuplicates 1, 2
End Sub

As you don't have headings, the 1 needed to be changed to 2 which is the enumeration for Header:=xlNo

Anyway, Jimmy has given you another option.

Cheerio,
vcoolio.
 
Upvote 0
Thanks for the feed back Howard. Glad I could help.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,215,086
Messages
6,123,038
Members
449,092
Latest member
ikke

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