Why do I get a VBA run-time error when assigning my Excel workbook file ?

Posted on 2014-09-16
Last Modified: 2014-09-16
Hi All,

I'm trying to save a workbook in my VBA procedure but it returns a Run-time error 9 (Subscript out of range) when it interprets the Set workbook line.

Why is this ? My workbook exists. Does it have to be open first, before saving ?

Here's my code.

   Dim wbMaster As Workbook
   Dim strFileName As String
   strFileName = "Master.xlsx"
   Set wbMaster = Workbooks(strFileName)
   Application.DisplayAlerts = False
   Application.EnableEvents = False
   wbMaster.SaveAs Filename:=strFileName
   Application.EnableEvents = True
   Application.DisplayAlerts = True

Open in new window

I get the error on the "Set wbMaster..." line.

Any ideas
Toco ?
Question by:Tocogroup
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
LVL 27

Expert Comment

ID: 40324859
strFileName must include the path too.
LVL 34

Accepted Solution

Norie earned 250 total points
ID: 40324875
Do you actually have a workbook with the name 'Master.xlsx' open when you run the code?

Author Comment

ID: 40324982
Hmmm....even when I enter the full path in strFileName I still get the same error. I've tried saving (and pointing at) the file elsewhere but the same thing happens  eg.    strFileName = "C:\Users\Toco\Master.xlsx"
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now


Author Comment

ID: 40324984
No, the Master workbook isn't open.
LVL 12

Assisted Solution

by:James Elliott
James Elliott earned 250 total points
ID: 40325003
You'll need something like this:

Dim wb as workbook
Dim strFileName as string

strFileName = "Master.xlsx"

set wb ="C:\yourPath\" & strFileName)

Open in new window

LVL 12

Expert Comment

by:James Elliott
ID: 40325004
You can then save when you close.

wb.Close True

set wb = nothing

Open in new window

LVL 34

Expert Comment

ID: 40325009
You can't refer to, or save, a workbook that isn't open.

Author Closing Comment

ID: 40325024
Thank you both. You were spot on with your solutions. Hope you don't mind if I split the points but your replies appeared at the same time.
It is strange because I've used my code before thinking it was the way to 'assign' a workbook object to a variable name. However, on reflection I've most likely used it in conjunction with the On Error Resume statement and never flagged the error.


Author Comment

ID: 40325031
You don't need the full path if the workbook resides in the same folder as the workbook module you are running the macro from.
LVL 34

Expert Comment

ID: 40325036
It's always best to specify the path.

If you don't VBA will look for the file in the current directory and that won't always be the same directory as the workbook you are running the code from resides.

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question