Issues with Copy/Paste Unique Values

MarsBars

New Member
Joined
May 21, 2014
Messages
27
I am having trouble with a pretty basic function. I need to copy everything in Column A (header excluded) and paste the unique values in column A in a different sheet. I chose to try to paste all of the values and then remove all of the duplicates. If there is an easier way to do this, please let me know.

Code:
    Sheets("Raw Data").Range("A:A").Copy
    
    Sheets("All Materials").Range("A3:A").Select
    ActiveRange.Paste
    Application.CutCopyMode = False
    ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes

I am getting the "application-defined or object-defined error" message and the break is occuring at this line:

Code:
 Sheets("All Materials").Range("A3:A").Select
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
The following seems to work:
Code:
Sub AAA()
    Sheets("Raw Data").Range("A:A").Copy
    Sheets("All Materials").Activate
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub

You have to paste into A1 as you're copying the whole column - any other row throws up an error.

Hope this helps.

Pete
 
Upvote 0
this will also work
Code:
    Sheets("All Materials").Range("A3:A").PasteSpecial xlPasteAll
    Application.CutCopyMode = False
    ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlYes

BTW, when using Select and Activate with Sheets and Ranges, the Sheet and ranges have to be on separate lines and each selected separately. Sheet first, then the range. Don't ask why.
 
Last edited:
Upvote 0
Pete, Thank you. That worked well. I assumed it was just something small like that.

JLG, Thanks for compressing it a little. I assumed there was a way to do it without all of the selecting and activating, but I didn't know the syntax. The only problem I had with that one is that it still threw the error code when I tried to use "A3:A". It worked fine when I changed it to "A:A". That's not a big problem though. I can work around it.

Thanks for the help and explainations guys.
 
Upvote 0
Pete, Thank you. That worked well. I assumed it was just something small like that.

JLG, Thanks for compressing it a little. I assumed there was a way to do it without all of the selecting and activating, but I didn't know the syntax. The only problem I had with that one is that it still threw the error code when I tried to use "A3:A". It worked fine when I changed it to "A:A". That's not a big problem though. I can work around it.

Thanks for the help and explainations guys.

The "A3:A" was copied from your original post and I should have caught it. Happy to help when I can.
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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