Set range value from another workbook

LGFN1

Board Regular
Joined
Jun 25, 2014
Messages
63
Why does this come up as a "Subscript out of range" error?
Note, testing this to another sheet in the original workbook works flawlessly, the problem arises when trying to do so to a second workbook.

Code:
SecondFile = "C:\Users\User\Documents\File2.xlsm"
Set SelRng = ActiveCell.Offset(0, 2).Resize(3, 2)
Workbooks.Open FileName:=SecondFile
Worksheets(SecondFile).Sheets(1).Range("A4:B6").Value = SelRng.Value
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Maybe:

Change this:

Code:
Worksheets(SecondFile).Sheets(1).Range("A4:B6").Value = SelRng.Value

To this:

Code:
Workbooks(SecondFile).Sheets(1).Range("A4:B6").Value = SelRng.Value
 
Upvote 0
The only thing you changed there is Worksheets to Workbooks? I tried it and the same error appears.
 
Upvote 0
That's one thing that would cause a subscript out of range error.

Maybe:

Code:
SecondFile = "C:\Users\User\Documents\File2.xlsm"
Set SelRng = ActiveCell.Offset(0, 2).Resize(3, 2)
Workbooks.Open FileName:=SecondFile
Sheets(1).Range("A4:B6").Value = SelRng.Value
 
Upvote 0
Try this.
Code:
SecondFile = "C:\Users\User\Documents\File2.xlsm"
Set SelRng = ActiveCell.Offset(0, 2).Resize(3, 2)
Set wb2  = Workbooks.Open( FileName:=SecondFile)
wb2.Sheets(1).Range("A4:B6").Value = SelRng.Value
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,040
Members
448,543
Latest member
MartinLarkin

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