Exit without saving in a macro

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a macro that closes a worksheet in excel and I want to close it and
not save (usually a box appears asking if I want to save before exiting and I
want to say no in the macro)
 
In the before close event add
ThisWorkbook.Saved = True

This is the flag that Excel uses to determine if the file needs to be saved
and the code above say that the file does not need to be saved. Don't place
any code after this line as that could change the spreadsheet and flip the
flag back again.
 
Hi Brian,
you can use:

Worksheets("yourworksheets").close Savechanges:= false

This is best way to do it, and never ask you about save changes.



"Brian" escreveu:
 
Your code does not work. Close is not a method of the worksheets object. It
is a method of the workbook. You could use

Thisworkbook.Close SaveChanges:=False

but it requires that the user close the workbook via a command button or
such to execute this line of code. If the user closes the workbook via the x
then this code never executes and the user is prompted about saving...
 
I have a macro that closes a worksheet

If it's a worksheet, and not a workbook, perhaps...

Sub Demo()
Application.DisplayAlerts = False
Worksheets(1).Delete
Application.DisplayAlerts = True
End Sub
 
Back
Top