Close Excel file without prompting user to save

G

Guest

I am a self-taught user of VB for Excel and I've come along pretty well, but
I'm getting stuck on something that seems like it should be fairly simple to
do. I have two files: File1 contains a macro that performs various
functions on File2. When the File1 macro is finished, I want File1 to close
automatically WITHOUT prompting the user if they wish to save (this file
should never be saved by the user). What can I do to force Excel to close
the file without an option to save?
 
A

Alan Howells

You can use
ThisWorkbook.Close SaveChanges:=False
or
Workbooks("File1").Close SaveChanges:=False
 
G

Guest

Here is an excerpt for the VBA help file...

This example closes the workbook that contains the example code and discards
any changes to the workbook by setting the Saved property to True.

ThisWorkbook.Saved = True
ThisWorkbook.Close

You can look up the saved property if you need more explanation...

HTH
 
D

Darrin Henshaw

Set Application.DisplayAlerts to False, to not show the save changes
dialog box. However, that will stop Excel from prompting it, but still
give the user the ability to save it. You might want to look at the
BeforeSave event, to try to stop them from saving.
 
T

Tom Ogilvy

Just a heads up for consideration:
I didn't test this particular case, but help says displayalerts = false
pursues the default action of the dialog.
Using displayalerts would suppress the prompt, but the default action would
be taken and that is to save the file.
 
V

Vasant Nanavati

Hi Tom:

I'm probably wrong <g>, but I seem to remember that this is the one case in
which Excel does not perform the default action (despite what Help says).

Regards,

Vasant
 
T

Tom Ogilvy

Hello Vasant,

Believe you are thinking of overwriting a file.

Application.displayAlerts = False
thisworkbook.SaveAs Filename:="C:\MyFolder\MyExistingFilename.xls"
Application.DisplayAlerts = True

In xl2003 help, at least, they point this exception out.
 
G

Guest

Both of these solution worked perfectly--thanks!

Alan Howells said:
You can use
ThisWorkbook.Close SaveChanges:=False
or
Workbooks("File1").Close SaveChanges:=False
 
V

Vasant Nanavati

I'm sure you're right, Tom ... my memory's not what it used to be!

Regards,

Vasant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top