Copy/Paste and Remove Duplicates

Hudco

Board Regular
Joined
Jan 4, 2006
Messages
127
Office Version
  1. 365
Hi Peoples,
I have constructed a macro to copy a column of data, paste to a new sheet and then remove duplicates. Everything worked fine until I tried to amend the code to paste just the values and not the formats. Also the remove duplicates removes the formats in the new sheet after the data finishes e.g. if original data is 50 rows and with duplicates removed becomes just 20 rows, the previous rows (21 to 50) will have their formats removed.

Code as follows:
Private Sub CommandButton3_Click()
Sheet3.Select
Range("A6", Cells(Rows.Count, "A").End(xlUp)).Copy
Sheet1.Activate
ActiveSheet.Range("A6").PasteSpecial Paste:=xlPasteValues
Application.Selection.RemoveDuplicates Columns:=1, Header:=xlNo
Range("A6", .Range("A6" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
ActiveSheet.Range("A6").Select
End Sub

The macro stops at the command ".Rows.Count" in the 7th line.

Can anyone help?
Thanks
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
hello,
should that line read,,
Code:
Range("A6", .Range("A" & .Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
 
Upvote 0
Thanks Pike,
Just altered the line (i.e. "A6" to "A") but it still stops at ".Rows.Count"

As I said before, everything worked fine until I added in "Paste:=xlPasteValues"

Cheers
 
Upvote 0
Try
Code:
Range("A6", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
 
Upvote 0
Thanks Fluff,
Previously just the ".Rows.Count" would be highlighted as an error in the macro. Now the whole line is highlighted as an error. But it did seem to activate through to the Delete part, just failed at the "Shift:=xlUp".

So I removed the line altogether and the macro now works except the cells where duplicates are removed from lose all their formats which are lines.

I can live with that but if any solution is still possible would be grateful.

Thanks, Clyde
 
Upvote 0
Hello,
it looks like a row count problem
the original code need .row but really required the row count befor remove dupes ran
Code:
Option Explicit

Private Sub CommandButton3_Click()
Dim lngRow As Long
With Sheet3
 .Range("A6", .Cells(.Rows.Count, "A").End(xlUp)).Copy
End With
 Sheet1.Activate
 Range("A6").PasteSpecial Paste:=xlPasteValues
 lngRow = Range("A" & Rows.Count).End(xlUp).Row
 Application.Selection.RemoveDuplicates Columns:=1, Header:=xlNo
 'Range("A6", Range("A" & Rows.Count).End(xlUp).row).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
 Range("A6", Range("A" & lngRow)).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
 Range("A6").Select
 End Sub
 
Upvote 0
Solution
Thanks Pike, it worked but formats were not restored on cells that were cleared. However I found a solution by copying formats of cells in adjacent row which are identical.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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