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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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