VBA Code for Copy & Sort

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
421
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance and I will let you know if it works once I get a response.

Given: Sheet 1 and Sheet 2 exist and Sheet 1 has data where Shee2 1 only has headings
Objective: Copy cells A2 through last entry in column A of Sheet 1 and paste it into Sheet 2 starting at A2 and then delete the duplicates

The error message I get is at ".Rows" of in the following line:

LastRow12 = .Range("A" & .Rows.Count).End(xlUp).Row

Code:
Sub Binder()

      Worksheets("Sheet1").Activate
    
      'Settings
      Dim WSBTEI As Worksheet
    
      Set WSBTEI = ThisWorkbook.Sheets("Sheet1")
    
     'Copy the First Column starting with cell A2 (i.e. don't copy the heading)
     
     WSBTEI.Columns(4).Copy Destination:=Sheets("Sheet2").Columns(1)
    
     'Remove Duplicates & Sort
                
    Worksheets("Sheet2").Activate
        
    Dim LastRow12 As Long
    LastRow12 = .Range("A" & .Rows.Count).End(xlUp).Row
    .Range("A2:A" & LastRow12).RemoveDuplicates Columns:=Array(1, 1), Header:=xlYes
    End With
   
End Sub
 
Last edited:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
See if you can work with this.

Code:
Sub Binder2()
Dim WSBTE1 As Worksheet. rw As Long
Set WSBTE1 = Sheets("Sheet1")
rw = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row + 1
WSBTE1.Range("D1", WSBTE1.Cells(Rows.Count, 4).End(xlUp)).AdvancedFilter _
xlFilterCopy, , Sheets("Sheet2").Cells(rw, 1), True
Sheets("Sheet2").Range("A" & rw).Delete xlShiftUp
End Sub

Your error with your code was caused by the period in front of Rows since there was no preceding with statement. You also have an End With which would give an error for the same reason.
 
Last edited:
Upvote 0
Thanks so much for this . Shortly before you posted this I modified my code to something similar to this go get it to work.
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,243
Members
448,555
Latest member
RobertJones1986

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