Copying Columns from 1 worksheet to another

KW

Board Regular
Joined
Jan 25, 2005
Messages
167
Does anybody know any better code than the following to copy certain columns of data from one worksheet to another, without the need to repeat the code for every column seperately?

Sub copyColumns()

Worksheets("Sheet1").Activate
Range("A1", Range("A1").End(xlDown)).Copy
Worksheets("Sheet2").Range("A1").PasteSpecial Paste:=xlValues

Range("C1", Range("C1").End(xlDown)).Copy
Worksheets("Sheet2").Range("B1").PasteSpecial Paste:=xlValues

Range("G1", Range("G1").End(xlDown)).Copy
Worksheets("Sheet2").Range("C1").PasteSpecial Paste:=xlValues

End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
How about this?
Code:
Sub copyColumns() 
Dim LastRow As Long

      LastRow=Worksheets("Sheet1").Range("A65536").End(xlUp).Row
      Worksheets("Sheet1").Range("A1:A" & LastRow).Copy
      Worksheets("Sheet2").Range("A1").PasteSpecial Paste:=xlValues 

      LastRow=Worksheets("Sheet1").Range("C65536").End(xlUp).Row
      Worksheets("Sheet1").Range("C1:C" & LastRow).Copy
      Worksheets("Sheet2").Range("B1").PasteSpecial Paste:=xlValues 

      LastRow=Worksheets("Sheet1").Range("G65536").End(xlUp).Row
      Worksheets("Sheet1").Range("G1:G" & LastRow).Copy
      Worksheets("Sheet2").Range("C1").PasteSpecial Paste:=xlValues 
   
End Sub
 
Upvote 0
Public Sub CopyColumnsV2()
Dim BotRow As Integer
Worksheets("Sheet1").Activate
With Sheets("Sheet2")
For Each Col In Array(1, 3, 7)
TargCol = TargCol + 1
BotRow = Cells(65536, Col).End(xlUp).Row
.Range(.Cells(1, TargCol), .Cells(BotRow, TargCol)).Value = _
Range(Cells(1, Col), Cells(BotRow, Col)).Value
Next Col
End With
End Sub
 
Upvote 0
Public Sub CopyColumnsV3()
Worksheets("Sheet1").Activate
With Sheets("Sheet2")
For Each Col In Array(1, 3, 7)
TargCol = TargCol + 1
.Range(.Cells(1, TargCol), .Cells(Cells(65536, Col).End(xlUp).Row, TargCol)).Value = _
Range(Cells(1, Col), Cells(Cells(65536, Col).End(xlUp).Row, Col)).Value
Next Col
End With
End Sub

Note: to change the source columns used just add or change Array(1, 3, 7)
example ... if the source was cols A,B,F,G then change to Array(1,2,6,7) :wink:
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
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