Close workbook without being prompted to save in VBA

J

J Wait

I am using the following code to close and save a workbook:

Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=True

I can't seem to get this to work without prompting me to save, even though I
did application.displayalerts = false and I told it to save in the
active.workbook.close command.
 
T

TG

I am using the following code to close and save a workbook:

Application.DisplayAlerts = False
ActiveWorkbook.Close SaveChanges:=True

I can't seem to get this to work without prompting me to save, even though I
did application.displayalerts = false and I told it to save in the
active.workbook.close command.

I made it work with

ActiveWorkbook.Save
ActiveWorkbook.Close

/Tommy
 
J

Jim Thomlinson

Do you have any "on close" event code. If so then it may be altering the
workbook which will prompt the save message. You can use Activeworkbook.Saved
= true to make the application believe that a save is not necessary...
 
J

J Wait

I tried what you said to begin with, and even though I told it to save before
I told it to close, it still prompted me to save on the close command. I
don't know what else to try. I've never run into this problem before.

I have also tried "ActiveWorkbook.Close True", and it still prompts me to
save.

To answer Jim's question, I don't have any "on close" event code, so I can't
try what he suggested.

Please let me know if you have any other ideas I could try. I'm hung up on
this one piece of the code!
 
T

TG

I tried what you said to begin with, and even though I told it to save before
I told it to close, it still prompted me to save on the close command. I
don't know what else to try. I've never run into this problem before.

I have also tried "ActiveWorkbook.Close True", and it still prompts me to
save.

To answer Jim's question, I don't have any "on close" event code, so I can't
try what he suggested.

Please let me know if you have any other ideas I could try. I'm hung up on
this one piece of the code!

The reason could be a formula containing NOW() or TODAY() as these are
changing ' on the fly' and therefore the worksheet believe it has
changed, and must be saved on closing..

Maybe this will work, I haven't tried yet

ActiveWorkbook.Save
ActiveWorkbook.Saved
ActiveWorkbook.Close

/Tommy
 
J

J Wait

Thank you so much Tommy and Jim! Here's what ended up working:

ActiveWorkbook.Save
ActiveWorkbook.Saved = True
ActiveWorkbook.Close

You solved a huge headache for me! Thanks!
 

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