Code for button to close workbook without saving.

skeeeter56

New Member
Joined
Nov 26, 2016
Messages
42
Office Version
  1. 2019
Platform
  1. Windows
I have a workbook that is used to produce a leave form. It all works from a list of peoples names on the worksheet Staff Information and 2 pop up calender's to add the dates.
Once the data is complete it is used to print a form. The data does not need to be saved, it is only used to produce and print this form.
It has 2 buttons one to print and one to clear clear so can produce another. I also have a close button, this is what I am having issues with.
I had code that when you run it indeed did not save or prompt to save, but it closed all excel workbooks that were open.
With the below code I am trying to close the just this workbook with out saving or prompting to save.
In essence this file is a template that needs to be blank when opened, it is also stored on a SharePoint site and that is where it is opened from.

leaveform.JPG


This is the code I currently have
VBA Code:
Private Sub CommandButton1_Click()
Application.DisplayAlerts = False
If Workbooks.Count < 2 Then
Application.DisplayAlerts = False
ThisWorkbook.Save
Application.Quit
Else
ThisWorkbook.Close SaveChanges:=False
End If

End Sub
I have tried varuious ways to achieve what I want but so far it has eluded me.
I am hoping somone may be able to assist
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
That code looks fine to me. It should only quit Excel if only one workbook is open.
 
Upvote 0
That code looks fine to me. It should only quit Excel if only one workbook is open.
HI RoryA yes it does work, in that if other workbooks are open it does not close them. But it still I think is saving when it is the only workbook open.
 
Upvote 0
But it still I think is saving when it is the only workbook open.

I was surprised you wanted to do that.
Replace this with the close statement you have in the else part.
"ThisWorkbook.Save"
Replace with
"ThisWorkbook.Close SaveChanges:=False"
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top