there is 2 events in my code i.e Before close,before save. in before save condition there are conditions to be filled before saving, else it wont allow to save. now while triggering before close, it will popup for save, dontsave, cancel message box. when selecting save, it will call before save function and it throws error message as like before save function. but after that file is closed.
Public Sub Workbook_BeforeClose(Cancel As Boolean)
Exit Sub
End Sub
I think i misunderstood what u tried to describe , but take a look if my code below can help you.
Public Sub Workbook_BeforeClose(Cancel As Boolean)
Select Case MsgBox("Select what do you want", vbYesNoCancel + vbExclamation, "Atention")
Case vbYes
Call Workbook_BeforeSave(True, False)
Case vbNo
Application.Quit
Case vbCancel
Cancel = True
End Select
Exit Sub
Related
I have an odd thing (I think) but probably doing something incorrectly here. I am trying to force the user to enter in certain information in excel. The msgbox comes up with the OK/Cancel buttons but when either is selected the workbook saves and exits. If they cancel then I want it to sit on the cell where the issue is. Any help would be appreciated
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'---------------------------
'-- Missing Employee Name --
'---------------------------
If Cells(5, 2).Value = Empty Then
If Not MsgBox("Employee Name missing! Pressing OK will exit without saving.", vbOKCancel, "The Mill") = vbOK Then
With Sheets("sheet1")
.Activate
.Cells(5, 2).Activate
End With
Cancel = True
Exit Sub
End If
End If
End Sub
I think the problem is that you aren't handling Workbook_BeforeClose, which triggers as you try to close the workbook.
So while the save is canceled, the close is not.
Moving everything to Workbook_BeforeClose and handling it there kind of works, until the user saves manually. So you will still have to handle that in the before save event.
This is an example:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Cells(5, 2).Value = Empty Then
If MsgBox("Employee Name missing! Pressing OK will exit without saving.", vbOKCancel, "The Mill") = vbOK Then
Application.DisplayAlerts = False
Application.Quit
Else
Sheets("Sheet1").Cells(5, 2).Activate
Cancel = True
End If
End If
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Cells(5, 2).Value = Empty Then
Cancel = True
MsgBox "Employee Name missing!"
Sheets("Sheet1").Cells(5, 2).Activate
End If
End Sub
The only caveat I have seen, is that Application.DisplayAlerts = False will affect other open workbooks.
I have a command button to save an Excel workbook in a particular format.
I want that the users are unable to save the workbook unless they click that command button.
To prevent the use of traditional saving options I used the code below:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "You can't save this workbook!"
Cancel = True
End Sub
How do I prevent traditional saving options but allow saving if the command button is clicked?
You can have a boolean flag to indicate whether or not saving is allowed and only set it to true when you call the macro that contains your logic for saving the workbook.
Add the following code under ThisWorkbook:
Public AllowSave As Boolean
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not AllowSave Then
MsgBox "You can't save this workbook!"
Cancel = True
End If
End Sub
Public Sub CustomSave()
ThisWorkbook.AllowSave = True
' TODO: Add the custom save logic
ThisWorkbook.SaveAs '....
MsgBox "The workbook has been saved successfully.", , "Workbook saved"
ThisWorkbook.AllowSave = False
End Sub
Note that you'll need to assign the CustomSave macro to the button that will be initiating the custom save.
Demo:
I already have a successful "custom save" macro to save-as with a date stamp. I just want to have a message box ask to run it when someone tries to manually save. I essentially need "yes" to run the macro, "no" to save normally, and "cancel" to exit sub.
However, whenever I file>save, or ctrl+s, it just saves without prompting.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim answer As VbMsgBoxResult
answer = MsgBox("Would you rather Save-As copy with date stamp?", vbYesNoCancel + vbQuestion + vbDefaultButton1, "You are overwriting the document!")
If answer = vbYes Then
Call filesave
ElseIf answer = vbNo Then
ActiveWorkbook.Save
Else
Exit Sub
End If
End Sub
You need to set the Cancel from the sub procedure's arguments to True in order to halt the current save operation.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim answer As VbMsgBoxResult
answer = msgbox("Would you rather Save-As copy with date stamp?", vbYesNoCancel + vbQuestion + vbDefaultButton1, "You are overwriting the document!")
If answer = vbYes Then
'Cancel the current standard save operation
Cancel = True
Call filesave
ElseIf answer = vbNo Then
'don't do anything; the standard save operation will proceed
Else
'Cancel the current standard save operation
Cancel = True
End If
End Sub
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?
I found this code and it works perfectly. But when I hit close button, dialog is shown "Do you weant to save changes", and if I choose Yes, an error comes up that I don't have a value in a cell A. And then my file is automatically closed.
How to prevent this, and to stay in the document?
My code is:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ActiveSheet.Range("A1").Value = "" Then
Cancel = True
Response = MsgBox("Please enter a value in A1", vbCritical, "Error!")
End If
End Sub
Have you tried using BeforeClose instead of BeforeSave?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ActiveSheet.Range("A1").Value = "" Then
Cancel = True
Response = MsgBox("Please enter a value in A1", vbCritical, "Error!")
End If
End Sub
Regards