Copy values multiple columns (non-adjacent) to another sheet

duranimal86

New Member
Joined
Jul 24, 2019
Messages
18
Office Version
  1. 365
Platform
  1. Windows
I have a workbook with 2 sheets that are identical, except one of them has a data connection that can be refreshed. I want to be refresh the data and then run this code to automatically copy those columns (as values) to the other sheet. I tried to combine into a single operation, but it wouldn't work with more than 2 column references. The code below works, but it is slow and i know there are much better options.

VBA Code:
Sub CopyValues()

Sheet3.Range("F:G").Value = Sheet2.Range("F:G").Value
Sheet3.Range("L:M").Value = Sheet2.Range("L:M").Value
Sheet3.Range("O:P").Value = Sheet2.Range("O:P").Value
Sheet3.Range("S:T").Value = Sheet2.Range("S:T").Value

End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Rather than using entire columns, can you find the last used row and use that to narrow the value assignments? For example, if column F always has a value in it to the very bottom row:
VBA Code:
Sub CopyValues()
Dim lastrow as Long
lastrow = Sheet2.Range("F" & Rows.Count).End(xlUp).Row

Sheet3.Range("F1").Resize(lastrow, 2).Value = Sheet2.Range("F1").Resize(lastrow, 2).Value
Sheet3.Range("L1").Resize(lastrow, 2).Value = Sheet2.Range("L1").Resize(lastrow, 2).Value
Sheet3.Range("O1").Resize(lastrow, 2).Value = Sheet2.Range("O1").Resize(lastrow, 2).Value
Sheet3.Range("S1").Resize(lastrow, 2).Value = Sheet2.Range("S1").Resize(lastrow, 2).Value

End Sub
 
Upvote 0
Solution
No, unfortunately it isn't a clean data table, so there are blank rows in the middle of what i need to copy.
 
Upvote 0
Blank rows in the middle isn't an issue. You just need to determine what is the absolute last used row on the original sheet (Sheet2). If that's column A or ZZ or whatever, use that column in the code to determine the last row. Otherwise you could just use something like the following to get the very last row # of that worksheet's used range:

lastrow = Sheet2.Range("A1").SpecialCells(xlLastCell).Row

Then you wouldn't need to adjust anything else in the code.
 
Upvote 0
Oh, ok. I tested your code and it worked and was A LOT faster than my original code. Thank you!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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