Remove duplicates from a variable range of cells.

Further to my previous questions, I am copying from one sheet to another, sorting, then removing duplicates.

I do not know who to tell it which range to use to remove the duplicates. So many of the examples on the web show multiple columns. I only need B3 down to the last filled cell in column B. I am also pretty sure my remove duplicates code isn't the most efficient.

Sub Macro1()
    Sheets("Summary").Select
    Dim DataRange As Range

Sheets("Data").Select
    Range("D2", Selection.End(xlDown)).Copy
    Sheets("Summary").Select
    Range("B3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Set DataRange = Range("B3:B" & lastRow)
    DataRange.RemoveDuplicates , xlYes
    ActiveWorkbook.Worksheets("Summary").Sort.SortFields.Add Key:=Range("B3"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Summary").Sort
        .SetRange Range("A3:R548")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub

Open in new window

LVL 22
Anne TroyEast Coast ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saqib Husain, SyedEngineerCommented:
Does it not work as-is?
0
Anne TroyEast Coast ManagerAuthor Commented:
This is another step, Saqib. And, again, thank you so much for your help.

I need to copy the data from Data!D to Summary!B and remove the duplicates. I do not know how to get the values now in D to remove the duplicates.
0
Roy CoxGroup Finance ManagerCommented:
Your code does not give lastRow a alue. Try this code, it removes unnecessary selecting but I cannot test it without seeing your workbook
Sub Macro1()
    Sheets("Summary").Select
    Dim DataRange As Range

Sheets("Data").Select
    Range("D2", Selection.End(xlDown)).Copy
    Sheets("Summary").Select
    Range("B3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Set DataRange = Range("B3:B" & lastRow)
    DataRange.RemoveDuplicates , xlYes
    ActiveWorkbook.Worksheets("Summary").Sort.SortFields.Add Key:=Range("B3"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Summary").Sort
        .SetRange Range("A3:R548")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub

Open in new window

0
Saqib Husain, SyedEngineerCommented:
These lines would filter column d to column Z while removing duplicates and then move that data to the other sheet.

    Range("D1:D" & range("D1").end(xldown).row).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("Z1"), Unique:=True
    Range("Z1").ClearContents
    Range(Range("Z2"), Range("Z2").End(xlDown)).Cut Sheets("Summary").Range("B3")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roy CoxGroup Finance ManagerCommented:
Glad we helped
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.