Copy and paste a column from one sheet to another

most

Board Regular
Joined
Feb 22, 2011
Messages
107
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. Mobile
I can't get the code below to work, anybody can see what the issue is? Or have a better way to solve it?

What I want the script to do is to copy data from a column to another sheet in a another workbook. Multiple times, so I need to reuse code to make it sustainable.
As you can see in the example below the first line is different between the workbooks, 2 vs 7.


Code:
Sub CopyPasteColumn()'Global settings
  AWB = ActiveWorkbook.Name
  DGW = "TheOtherSide"
'Column 1
FromSheet = "Old Customer"
ToSheet = "New Customer"
FromColumn = 3
ToColumn = 4
    LastRow = Cells(Rows.count, FromColumn).End(xlUp).row
    Application.Workbooks(AWB).Worksheets(FromSheet).Range(Cells(2, FromColumn), Cells(LastRow, FromColumn)).Copy _
     Application.Workbooks(DGW).Worksheets(ToSheet).Range(Cells(7, ToColumn), Cells(LastRow, ToColumn))
'Column 2
FromSheet = "Old Customer"
ToSheet = "New Customer"
FromColumn = 2
ToColumn = 1
    LastRow = Cells(Rows.count, FromColumn).End(xlUp).row
    Application.Workbooks(AWB).Worksheets(FromSheet).Range(Cells(2, FromColumn), Cells(LastRow, FromColumn)).Copy _
     Application.Workbooks(DGW).Worksheets(ToSheet).Range(Cells(7, ToColumn), Cells(LastRow + 5, ToColumn))
'Column 3
'...And so on...


End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Does this do what you need for the first column?
Code:
Sub most()
   Dim FromWs As Worksheet, ToWs As Worksheet
   
   Set FromWs = ActiveWorkbook.Sheets("Old Customer")
   Set ToWs = Workbooks("TheOtherSide.xlsm").Sheets("New Customer")
'Column 1
FromColumn = 3
ToColumn = 4
   lastrow = FromWs.Cells(Rows.Count, FromColumn).End(xlUp).Row
   FromWs.Range(Cells(2, FromColumn), Cells(lastrow, FromColumn)).Copy _
      ToWs.Cells(7, ToColumn)

End Sub
 
Upvote 0
Somebody should name a road "Fluff", THANKS!
 
Upvote 0
Ok we can now expand that like
Code:
Sub most()
   Dim FromWs As Worksheet, ToWs As Worksheet
   Dim Ary As Variant
   Dim i As Long, Lrw As Long
   
   Set FromWs = ActiveWorkbook.Sheets("Old Customer")
   Set ToWs = Workbooks("TheOtherSide.xlsm").Sheets("New Customer")
   Ary = Array(3, 4, 2, 1)
   
   For i = 0 To UBound(Ary) Step 2
      Lrw = FromWs.Cells(Rows.Count, Ary(i)).End(xlUp).Row
      FromWs.Range(Cells(2, Ary(i)), Cells(Lrw, Ary(i))).Copy _
         ToWs.Cells(7, Ary(i + 1))
   Next i
End Sub
Which should do the first two columns
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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