Removing Duplicates

Haree

Board Regular
Joined
Sep 22, 2019
Messages
146
Office Version
  1. 2016
Hello All, I am quite new to vba and learning one by one. Kindly excuse me if this is a very elementary question and forgive me for my ignorance.
Basically I have a macro which adds names to Column A sorts it in ascending order and then removes duplicates. I am attaching the code below. Everything works fine except for removing duplicates.
Your help would be greatly appreciated . Thanks in advance

VBA Code:
Sub Button1_Click()
Application.ScreenUpdating = False
Dim lastrow As Long, name As Range
Set name = Worksheets("Names").Range("J7")

Worksheets("Names").Select
    If name <> "" Then
        name.Copy
        lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
        Range("A" & lastrow + 1).Select
        ActiveSheet.PasteSpecial xlPasteAllExceptBorders
    End If

Worksheets("Names").Sort.SortFields.Clear
Range("A" & lastrow).Sort , Key1:=Range("A1"), order1:=xlAscending, Header:=xlYes

ActiveSheet.Range("A1", "A" & lastrow).RemoveDuplicates , Header:=xlYes
name.Select
Application.ScreenUpdating = True
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Here is some code for removing dupes



VBA Code:
Sub Rmv_Dupe_Rows()

    Application.ScreenUpdating = False

    Dim clmn As String
    Dim col As Long

'   Prompt for column letter
    clmn = Application.InputBox("Enter the letter designating" & vbCrLf & "the column where the duplicate" & vbCrLf & "data is located.")
'   Convert to column number
    col = Cells(1, clmn).Column
    
    ActiveSheet.UsedRange.RemoveDuplicates Columns:=col, Header:=xlGuess
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Thank you @alansidman . I had to use .RemoveDuplicates columns: , I had skipped the columns part. Thank you for your help
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,334
Members
449,077
Latest member
Jocksteriom

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