Copy directly to variable

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
537
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Is it possible to use the copy method and assign the results directly to a variable which can be used to later in a macro?

I want to copy several cells and ranges from various sheets in one workbook with the results of each copy being assigned to a different variable. Then later in the code I want call those variables and paste the contents into various sheets in a different workbook.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi KGee,

You could try this:

Code:

Sub test()
With Workbooks("Book3").Sheets(1)
For a = 1 To 3
ThisWorkbook.Sheets(1).Cells(4, a) = .Cells(4, a)
Next a
End With
End Sub

Code:

If you don't want to copy the values directly, you could first put them into a vairable, then put them into the desired WB.

ColinKJ
 
Upvote 0
Hi Colin,

Can you elaborate on how I could use your code for my needs? I'm not sure how I would go about assigning the specific cells I want to my variables.

What I'm looking to do is something like so:
open "Book1" and assign all the variables I need at one time
copy cell C5 from Sheet1 to var1
copy range B6:B8 from Sheet2 var2
copy cell D2 from Sheet3 to var3
etc..

then open "Book2" and place them accordingly
paste var1 in cell B2 of Sheet2
paste var2 in range E6:E8 of Sheet3
paste var3 in cell F4 of Sheet1

Thanks - Kevin
 
Upvote 0
Hi KGee,

The following assumes both workbooks are open, and the code is in Workbook 1.

Code:

Sub CopyData()
With Workbooks("Book 2")
.Sheets(2).Cells(2, 2) = ThisWorkbook.Sheets(1).Cells(5, 3)
For a = 6 To 8
.Sheets(3).Cells(a, 5) = ThisWorkbook.Sheets(2).Cells(a, 2)
Next a
.Sheets(1).Cells(4, 5) = ThisWorkbook.Sheets(3).Cells(2, 4)
'Etc
End With
End Sub

Code:

ColinKJ
 
Upvote 0
This will set myArray to a 2D array filled with the values in the first range.
It then puts those values in the second range.
Code:
Dim myArray as Variant

myArray = Workbooks("Book1").Sheets("sheet1").Range("A1:B3").Value

Workbooks("BookTwo").Sheets("sheet2").Range("C11:D13").Value = myArray
 
Upvote 0
Guys,

I will try these out tomorrow when I have time. Thanks for the help.

Kevin
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,780
Members
449,049
Latest member
greyangel23

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