Copy/Paste and remove duplicates

Apple08

Active Member
Joined
Nov 1, 2014
Messages
450
Hi all

I have done a macro to copy data from sheet2 to sheet 3 then remove duplicates, however it doesn't work, please could anyone help?

Code:
[FONT=Calibri][SIZE=3][COLOR=#000000]Sub Reconcile()[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000] [/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    Dim LastRow AsLong[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    LastRow =Range("A" & Rows.Count).End(xlUp).Row[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    [/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    Sheet2.Select[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]   Range("F2:F" & LastRow).Select[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    Selection.Copy[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]   Sheets("Reconcile").Select[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]   Range("C2").Select[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    ActiveSheet.Paste[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]   Application.CutCopyMode = False[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]   ActiveSheet.Range("$C$2:$C" & LastRow).RemoveDuplicatesColumns:=1, Header:=xlNo[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]   Range("A2").Select[/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    [/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]    [/COLOR][/SIZE][/FONT]
[FONT=Calibri][SIZE=3][COLOR=#000000]End Sub[/COLOR][/SIZE][/FONT]
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How about
Code:
Sub Apple08()
   Dim LastRow As Long
    
   With Sheet2
      LastRow = .Range("A" & Rows.Count).End(xlUp).Row
      .Range("F2:F" & LastRow).Copy
   End With
   With Sheets("Reconcile")
      .Range("C2").PasteSpecial
      Application.CutCopyMode = False
      .Range("$C$2:$C" & LastRow).RemoveDuplicates Columns:=1, Header:=xlNo
   End With
End Sub
 
Upvote 0
If Fluff's suggestion does not work for you, can you expand on this as it provides no detail at all.
.. however it doesn't work,
- Code gives error message? (What message?)
- Wrong result? (samle data and expected result?)
- Excel crashes?
- Code does nothing?
- Something else?
- etc
 
Upvote 0
Remove Duplicates only works reliably if the data is sorted first. If the data when pasted into column C of 'Reconcile' is not sorted then I suggest that you include a sort of that column in your code before the RemoveDuplicates line.
If you want to see an example of what I am referring to look here.
 
Last edited:
Upvote 0
Hi Peter, could you tell me what the sorting code should be added to my macro to make it more reliable please?
 
Upvote 0
How about
Code:
Sub Apple08()
   Dim LastRow As Long
    
   With Sheet2
      LastRow = .Range("A" & Rows.Count).End(xlUp).Row
      .Range("F2:F" & LastRow).Copy
   End With
   With Sheets("Reconcile")
      .Range("C2").PasteSpecial
      Application.CutCopyMode = False
      With .Range("$C$2:$C" & LastRow)
         .Sort key1:=.Resize(1), order1:=xlAscending, Header:=xlNo
         .RemoveDuplicates Columns:=1, Header:=xlNo
      End With
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

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