Prevent save changes dialog box upon closing - excel

I have spreadsheet which uses events upon opening, therefore anytime I try to close the file, the save changes dialog box appears.
Is there a way to prevent this?

You can add a function in VBA to ThisWorkbook to let Excel think that the file has been saved. This will prevent any save prompts.
If you don't want to save your workbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Me.Saved = True
End Sub
If you want to save your workbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Me.Save
End Sub

Here documentation for deleting work sheet.
So, you need to use as follow:
'Stopping Application Alerts
Application.DisplayAlerts=FALSE
'~~~~~~deleting sheet~~~~~
'Enabling Application alerts once we are done with our task
Application.DisplayAlerts=TRUE

Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Close SaveChanges:=False
End Sub

Related

Detect if Save event is triggered manually or by autosave

I'm using a small piece of code to export the VBProject components before saving the workbook.
Private Sub App_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Wb.HasVBProject Then
If Wb.VBProject.Protection = vbext_pp_none Then
If vbYes = MsgBox("Do you want to export the VBProject components?", vbYesNo) Then
ExportWB Wb ' call a function to export the data
End If
End If
End If
End Sub
This code is working fine, but if the Autosave is on the user (me) is asked every few second if it wants to export the project.
I can check if Autosave is On or Off with Wb.AutoSave, but is there a way to check if the event was triggered manually or by Autosave, in order to run the code only when someone require a save, not for the Autosave events?
Vincent
Using ThisWorkbook.AutoSaveOn = False, the effect should be gone. You can place this line e.g. into ThisWorkbook's event Private Sub Workbook_Open.
You can get the info about if AutoSave is currently enabled for this file, from ThisWorkbook.AutoSaveOn (returns True for enabled, or False for disabled).

VBA - Application.Visible = False on Workbook_Open() does not stay False if another workbook opens

The code below works fine for hiding the workbook and showing the UserForm on workbook open. But when I open another workbook alongside it the workbook then appears, as if the application.visible is changed to true in the background. Is there a way to prevent this?
Private Sub Workbook_Open()
ThisWorkbook.Application.Visible = False
UserForm1temp.Show
End Sub
Thank you for the help in advance, I will mark correct answers accordingly and accept the final.
Make the window that that the workbook is in invisible as well as the application
Private Sub Workbook_Open()
Application.Visible = False
ThisWorkbook.Windows(1).Visible = False
UserForm1.Show
End Sub
A safer approach is to use the WindowState property than using Application.Visible property, if the intent is that user only sees the form. Not a robust solution but get the job done.
Private Sub Workbook_Open()
Application.WindowState = xlMinimized
AutomatedEmailSender.Show
End Sub
And then, moving the closing workbook code to the UserForm_Terminate event.
Private Sub UserForm_Terminate()
ThisWorkbook.Close , False
End Sub
Application.workbooks.application.visible=false

Workbook Prompts for Changes Upon Closing When no Changes Were Made

I have an Excel file that runs some ActiveX components. The downside is that it asks to save each time I close the file, even when I didn't make any changes.
I've tried adding the following code to the ThisWorkbook object:
Private Sub Workbook_Open()
ActiveWorkbook.Saved = True
End Sub
However, this doesn't seem to work. It still asks me to save the file when I try to close it without making changes.
Any help?
Ah, yes I have had to deal with this issue myself. Since these components run AFTER the workbook is opened (and after your Workbook_Open() event), you need to run your code when you're closing your workbook.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Saved = True
End Sub
Should be placed in the ThisWorkbook code module.
To avoid accidentally not saving the workbook when you actually update it, you could try setting your own global variable flag - which should be located in a standard module and have the Public scope. Afterwards, you can use the Workbook_SheetChange event to set this flag when you actually change any content within the workbook:
Standard Module: Add Pub Flag
Public bPromptSave as Boolean
Workbook Module: Event Handlers
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
bPromptSave = True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
if not bPromptSave then ThisWorkbook.Saved = True
End Sub

Closing application only thru VBA

I have this workbook and I want to make it look like a program.
I also want people using it to only be able to quit the application thru a specific command button.
Here is the code I have
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "{ESC}"
Cancel = True
MsgBox "Please use the QUIT button to save and close the database."
End Sub
And for the quit button:
Sub SAVE_CLOSE
ActiveWorkbook.Save
If Workbooks.Count < 2 Then
Application.Quit
Else
ActiveWorkbook.Close
End If
End Sub
My problem is that when the user quit the application thru this button, it triggers the Private Sub Workbook_BeforeClose event and cancel the quitting process. How would you fix this?
Thanks!
Just set a flag in the button handler and test for it in the BeforeClose event:
In ThisWorkbook...
Public okToClose As Boolean '<--module level.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not okToClose Then
Application.OnKey "{ESC}"
Cancel = True
MsgBox "Please use the QUIT button to save and close the database."
End If
End Sub
...and in the handler...
Sub SAVE_CLOSE()
ActiveWorkbook.Save
ThisWorkbook.okToClose = True
If Workbooks.Count < 2 Then
Application.Quit
Else
ActiveWorkbook.Close
End If
End Sub
Note that you should avoid calling ActiveWorkbook.Close - use a hard reference instead. That's a really good way to destroy somebody's day's worth of work...
Since Workbook_BeforeClose() is called whenever the workbook is about to close (whether it's your method or not) you'll need some way within that method to know that it was your button that called it. Perhaps a global variable, or a value in a given cell on a hidden sheet?

Prevent Saving in excel couldn't be saved?

This may sound silly but I'm trying to prevent saving in Excel Workbook by using the code below in ThisWorkbook:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.ThisWorkbook.Saved = True
End Sub
However, after pressing Ctrl + s, and close the workbook, when reopening it, the code wasn't saved.
Any idea why this happen ?
This is a good thing - it means your code is working right. Your code prevented you from saving the workbook yourself. I have this in a couple applications I have made, the way around it is to make a sub as follows:
sub saveTheWB()
Application.EnableEvents = False
ThisWorkbook.Save
Application.EnableEvents = True
end sub
You have to run this sub every time you want to save.
That's right because the Workbook_BeforeSave event handler is firing and cancelling the save operation.
In the Immediate window of The IDE, run Application.EnableEvents = False before saving your workbook and it will save with the code.
Don't forget to run Application.EnableEvents = True after!

Resources