alias for workbook

  • Thread starter Thread starter sandy
  • Start date Start date
S

sandy

Why i got error when setting alias for workbook?
Dim wbk As Workbooks
Set wbk = Workbooks("Book1.xls")
 
because you dimensioned wbk as workbooks with an "s". It should be

Dim wbk as Workbook
 
but i got an error "subscription out of range" if i use
following code:

Sub auto_open()
Dim wbk As Workbook
Set wbk = Workbooks("book2.xls")
end sub
 
That means you don't have an open workbook named Book2.xls

If you just created a workbook and it is named book2 and you haven't save
it, you would refer to it as

set wbk = Workbooks("book2")

an unsaved, new workbook does not have an .xls extension.
 
so is it my excel problem?
because i did created the excel file named book2.xls
and
tried your method
delete my book2.xls and create one using "book2"

but still got an error
 
If you have a workbook named book2.xls in the same instance of excel, then
your code should work fine.

I created a workbook and saved it as book2.xls. I left it open.

I ran this code:

Sub auto_open()
Dim wbk As Workbook
Set wbk = Workbooks("book2.xls")
MsgBox wbk.Name
End Sub

with no problem.

If there is no open workbook named book2.xls you will get an error that says
error '9', subscript out of range.
 
Back
Top