Unable to Set workbook variable

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
I am trying to set a Workbook variable with the following

Code:
Public sendBook as Workbook
Public bookName as String
Set sendBook = Workbooks(bookName & ".xls")
End Sub
I keep getting 'subscript out of range' errors on this
I know that the string variable is good because I have tested it by having the code paste it into a range. I have also tried pasting the string and then using the range value but nothing works. Can anyone tell me what I am doing wrong?
Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
the workbook needs to be open. What is the value of variable bookname?
 
Upvote 0
In this instance it is a number derived from the Now() function. The workbook that creates the variable is indeed closed at this point in the code...but does not the fact that the variable is public eliminate the need to have the workbook open. Also, I have tried pasting the variable into the workbook that holds the code and then using that range value to set the w/b varialbe and it still does not work.
 
Upvote 0
Sorry, you really can't set a reference to a workbook that is closed. Workbooks(x) refers to the workbooks collection, and only open workbooks are members of that collection.
ξ
 
Upvote 0
Well I am still a bit confused...but that did seem to work.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

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