Solved

How do I fix  this 'subscript out of range error' ?

Posted on 2010-08-20
9
351 Views
Last Modified: 2012-06-27
I have a workbook before close macro that produces a 'subscript out of range' error when it gets to the code below. As soon as I click out of the error, both books close. But other than resorting to 'On Error Resume Next', how do I write it so that it closes both books without the error?

Thanks,
John
Windows("AllConfigs.xls").Close Saved = True
ThisWorkbook.Close Saved = True

Open in new window

0
Comment
Question by:gabrielPennyback
[X]
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
9 Comments
 
LVL 20

Accepted Solution

by:
Ardhendu Sarangi earned 125 total points
ID: 33490030
check the name of the file. it might be incorrect...

if not, check this code..

Workbooks("AllConfigs.xls").Close Saved = True
ThisWorkbook.Close Saved = True

see if this works.

- Ardhendu
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 125 total points
ID: 33490045
Use Workbooks not Windows.

If the file is new, it will not have an extension, like Workbooks("Book1")
If it is an existing file, the title changes - Workbooks("Book1.xls")
0
 
LVL 5

Assisted Solution

by:jklmn
jklmn earned 125 total points
ID: 33492704
Workbooks("AllConfigs.xls").Close True
ThisWorkbook.Close True
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 1

Author Comment

by:gabrielPennyback
ID: 33492977
Thanks for pointing out my "Windows" typo.  
Maybe there's something wrong with my computer but as long as I have the code 'ThisWorkook.Close True' anywhere in the macro, I get the error. If you take that line away, my calling workbook still closes, but i get the "Do you want to save ..." alert, which i would like to avoid. So I tried using "Appilcation.DisplayAlerts = False," but i still got the alert. Is there some other line of code that will suppress the alert without the line 'ThisWorkook.Close True'? If there is, then that will do it.
 Please create a file named 'AllConfigs.xls' , and try closing it from your 'Book1.xls' and let me know if you get the same results.
Thanks,
John
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 33492992
Okay, I just discovered that 'On Error Resume Next' solves the problem. But that always seems like kind of a cop out. Is there any other way to fix it?
Thanks,
John
0
 
LVL 5

Expert Comment

by:jklmn
ID: 33493415
The error 'subscript out of range error' should happen in ThisWorkbook.Close True because there is no subscript at all here.
0
 
LVL 5

Expert Comment

by:jklmn
ID: 33493417
Sorry I meant
The error 'subscript out of range' should not happen in 'ThisWorkbook.Close True' because there is no subscript at all here.
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 125 total points
ID: 33499470
Do you have a BeforeSave event? What if you use:
ThisWorkbook.Save
ThisWorkbook.Close False
0
 
LVL 1

Author Closing Comment

by:gabrielPennyback
ID: 33686595
I'm good now, thanks!

- John
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

739 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