Solved

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

Posted on 2010-08-20
9
345 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:
pari123 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now