I would like a dialog box to appear when someone attempts to close an important Excel sheet. Namely I would like it to ask "Have you logged all Changes?" giving them the opportunity to click "Yes" or "No". If they click "Yes" the sheet will close, but if they Click "No" it will ask them to "Please log all changes in the Change Notes section".
Any and all help greatly appreciated.
This is the code you are looking for. It asks the question, and if No is clicked, it cancels the closing of the workbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ureply = MsgBox("Have you Saved All Changes?", vbYesNo)
If ureply = vbNo Then
Cancel = True
End If
End Sub
This code needs to go on 'ThisWorkbook' (Shown above the word 'Modules') on your VBA editor
Related
As the title states, I am trying to create a button to clear a range of cells. Prior to clearing the cells, I have a dialog box pop up to confirm the selection. Here is my code:
Private Sub CommandButton1_Click()
If MsgBox("THIS WILL CLEAR EVERYTHING IN THE CART! Are you sure?", vbYesNo) = vbNo Then Exit Sub
Range("A28:AA47").ClearContents
End Sub
The code works when I'm in Design Mode and press "Play" but the button won't work on my sheet when I'm out of Design Mode.
Is the code completely ending itself when I select "No", to the point where it will not run again even if I hit the button again?
Your code looks a little incomplete. Try this instead:
EDIT: Cleaned it up a little more. No need for a vbNo, as if you don't select yes, the subroutine will simply end.
Private Sub CommandButton1_Click()
If MsgBox("THIS WILL CLEAR EVERYTHING IN THE CART! Are you sure?", vbYesNo) = vbYes Then
Range("A28:AA47").ClearContents
End If
End Sub
I've been researching how to disable "Save As" in Excel and it seems possible. I have a macro that loops through a bunch of Excel files making changes. In that macro I would like to disable "Save As" for all of the files. Is this possible?
Using application object. You just add this lines to the code that loops in every file or workbook so it disable "save as" for each one :
Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls("Save As...").Enabled = False
Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls("Save").Enabled = False
Tell me how it goes.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "You can't save this workbook!"
Cancel = True
End Sub
I have an excel sheet which is protected, since the sheet is protected I don't want the user to save it, and I don't want the save the sheet prompt to appear when someone closes the workbook. Till now I have been using this:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = True
Application.DisplayAlerts = False
End Sub
Using this code, save feature is disabled but the prompt is still appearing
Similar problem: Since the workbook is protected whenever someone tries to change the cell content it displays an alert, I want to disable that prompt message as well.
Can someone help me to fix this
L42 has already answered part of your question.
I want to disable that prompt message as well.
Do this. While protecting the sheet, unckeck the option Select Locked Cells. Now that will take care of the keyboard input while the sheet is locked and protected.
As for mouse inputs i.e the prompt showing up when you double click on the cell, use this :)
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
End Sub
I have a button on my form that clears the entire 8 sheet workbook. I do want to clear it occasionally, but I would hate to do it by accident. I've tried googling it, but every result I've found assumes I have a much firmer grasp of VBA than I do. How can I make it so when the button is clicked a Dialog box pops up saying "This will erase everything! Are you sure? [Continue] [Cancel]"?
Thanks.
On your existing button code, simply insert this line before the procedure:
If MsgBox("This will erase everything! Are you sure?", vbYesNo) = vbNo Then Exit Sub
This will force it to quit if the user presses no.
Create a new sub with the following code and assign it to your button. Change the "DeleteProcess" to the name of your code to do the deletion. This will pop up a box with OK or Cancel and will call your delete sub if you hit ok and not if you hit cancel.
Sub AreYouSure()
Dim Sure As Integer
Sure = MsgBox("Are you sure?", vbOKCancel)
If Sure = 1 Then Call DeleteProcess
End Sub
Jesse
Just make a custom userform that is shown when the "delete" button is pressed, then link the continue button to the actual code that does the deleting. Make the cancel button hide the userform.
Using VBA in Excel 2003, I'm trying to cancel an Application.OnTime event using the following code:
Application.OnTime EarliestTime:=varNextRunTime, Procedure:="SomeMethod", Schedule:=False
where varNextRunTime is a global variable containing the next time it is due to run. This code runs in the Workbook_BeforeClose event handler so is always run when the workbook is closed, which is my intention.
However, if the user tries to close the workbook, but changes their mind and hits the cancel button when prompted to Save (Yes, No, Cancel), the Application.OnTime event is still cancelled. BeforeClose is always run before they decide to hit cancel, so has anyone got any ideas how I can only cancel the Application.OnTime event when the workbook is closed?
Here's some ideas
http://www.dailydoseofexcel.com/archives/2004/06/16/beforeclose-vs-beforereallyclose/
Check the Saved property of the Workbook in your event handler. If the workbook is unsaved then display your own dialog to find out if the users wants to save changes, not save changes or cancel.
Here's some rough code. Obviously uncomment the line which deals with the Application.OnTime part and change the MsgBox title to something suitable
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim response As Integer
If Not (Me.Saved) Then
response = MsgBox("Do you want to save changes to '" & Me.Name & "'?", vbYesNoCancel, "put title here")
If (response = vbCancel) Then
Cancel = True
ElseIf (response = vbYes) Then
Me.Save
End If
End If
If Not (Cancel) Then
' Application.OnTime EarliestTime:=varNextRunTime, Procedure:="SomeMethod", Schedule:=False
End If
End Sub
Investigate using:
Application.Quit
If you find this command results in the Excel program remaining open although the document has closed, you may want to follow with
ActiveWorkbook.Close False
I'm not in position to test this or give more insights, unfortunately.
A bit late to the show but here is a simple solution that I've come across (and tested):
If a user deactivates the workbook by closing it, the workbook will still remain the ActiveWorkbook when the Workbook_WindowDeactivate event fires. If the user deactivates the workbook by switching to another workbook, then the new workbook will become the ActiveWorkbook by the time Workbook_WindowDeactivate fires. You can use this behavior to determine the action that caused the event to fire:
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
If Application.ActiveWorkbook.Name = Me.Name Then
'Your code here
End If
End Sub