Solved

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

Posted on 2010-08-20
9
350 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
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

679 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