Macro to Close Workbook Without Save Any Changes Made

T

Tyven Bong

How to make macro to close a workbook without save any changes that made
before the function CLOSE.
 
J

Joel

first you can open the workbook in readonly mode to make sure no changes are
made to the workbook. Second when you save the workbook use

set bk = workbooks.open(filename:="book1.xls")

'your code here


bk.close savechanges:=False
 
A

AltaEgo

From VBA help:

Remarks
Closing a workbook from Visual Basic doesn't run any Auto_Close macros in
the workbook. Use the RunAutoMacros method to run the auto close macros.

Example
This example closes Book1.xls and discards any changes that have been made
to it.

Workbooks("BOOK1.XLS").Close SaveChanges:=False
 
A

AltaEgo

Or kill the lot

Sub QitNoSave()

Application.DisplayAlerts = False
Application.Quit 'closes Excel

End Sub
 
P

Patrick Molloy

the application asks if you want to save if there have been any changes.

WB.Close False

is the easiest way to overcome this - where wb is the workbook obkect set to
the workbook we're dealing with.
Also , we could clear the dirty flag which tells excel whether the sheets
have changed or not in the ThisWorkbook's before close event. A value of
False would trigger the application alert to save.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Saved = True
End Sub
 

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