Set a reference to another workbook

nigelk

Well-known Member
Joined
Aug 30, 2008
Messages
535
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?
 

Some videos you may like

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,723
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,819
Messages
5,598,293
Members
414,223
Latest member
Accountant2B

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
Top