Set a reference to another workbook

nigelk

Well-known Member
Joined
Aug 30, 2008
Messages
537
Hi all

I have the following code

Code:
Workbooks.Open FileName:="C:\Sports\Golf.xlsm"

which works fine, however, as the the workbook is already open all I want to do is set a reference to it so I can write to it later. This gives a "Subcript out of range" error (Sportbook is dimmed as a workbook)

Code:
  Set Sportbook = Application.Workbooks("C:\Sports\Golf.xlsm")


What am I doing wrong?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
HTH. Dave
Code:
Dim Ofsobj As Object, NewWb As Object
Set Ofsobj = CreateObject("Scripting.FileSystemObject")
Set NewWb = Ofsobj.GetFile("C:\Sports\Golf.xlsm")
Workbooks.Open Filename:=NewWb
'to use example
Workbooks(NewWb.Name).Sheets("sheet1").Cells(1, "A").Value = _
          ThisWorkbook.Sheets("sheet1").Cells(1, "A").Value
Set NewWb = Nothing
Set Ofsobj = Nothing
 
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