Shutdown Excel through VBA?

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
It needs macros to be enabled...

Right mouse over the excel icon to the left of file and select view code, then paste this in;

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Saved = True
End Sub
 
Upvote 0
whatever you initialised the excel.application refer to it by that, mine was XL.

If XL Is Nothing Then
MsgBox ("XL is already closed")
Else
XL.DisplayAlerts = False
XL.Quit
XL.Visible = False
Set XL = Nothing
Set ws = Nothing
End If

Thats it and it works for me perfect.
Paul
 
Upvote 0
Eh...? Doesn't work...

Let me rephrase my needs:
i want to fit a command button into a workbook that will instantly close excel, no questions asked
 
Upvote 0
Hello

Do you mean close the workbook or close excel completely?

Code:
activeworkbook.close (0)

will close the active workbook.
 
Upvote 0
ok, are you doing this using macros or are u using visual basic software, if u are using the software u need to create instances of excel.application and maybe even excel.worksheet also.

eg.
Dim WithEvents XL As Excel.Application
Dim WithEvents Wb As Excel.Workbook
Dim WithEvents ws As Excel.Worksheet

To close these instantly say attaching this cose to a button all u have to do is put the code in i suggested, u could alos just put in this to make it easier:

XL.DisplayAlerts = False
XL.Quit
XL.Visible = False
Set XL = Nothing
Set ws = Nothing
 
Upvote 0
Private Sub CommandButton1_Click()
Application.DisplayAlerts = False
Application.Quit
End Sub


HTH

GaryB
 
Upvote 0

Forum statistics

Threads
1,215,588
Messages
6,125,691
Members
449,250
Latest member
azur3

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