I am building a code which gives warning when the workbook is closed. By default excel warns only once (that too if current changes are not saved). I want warning to come twice. First time it should ask are you sure? when the person hits yes, again system should ask are you sure.
So far my code is as follows, but it is not working properly. With the below code, warning is only displayed once. Please can someone help?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim mbResult As Integer
Dim wkb As Workbook
Set wkb = ActiveWorkbook
mbResult = MsgBox("Are you sure you want to exit this program?", _
vbYesNo)
Select Case mbResult
Case vbYesNo
MsgBox "You are about to exit this program, are you sure?"
Case vbYes
Cancel = True
Case vbNo
' Do nothing and allow the macro to run
Exit Sub
End Select
End Sub
you can directly handle the return value of a MsgBox like this:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Select Case MsgBox("Are you sure you want to exit this file?", vbYesNo)
Case vbYes
Cancel = MsgBox("You are about to exit this program, are you sure?", vbYesNo) = vbNo
Case vbNo
Cancel = True
End Select
End Sub
or, which is equivalent:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = MsgBox("Are you sure you want to exit this file?", vbYesNo) = vbNo
If Not Cancel Then Cancel = MsgBox("You are about to exit this program, are you sure?", vbYesNo) = vbNo
End Sub
You could use a Workbook_BeforeClose event to prompt the user to check if they really want to do something.
Both these two codes below ask the user twice for confirmation if they want to close the workbook. The first one use the Workbook_BeforeClose event, so you will need to do something in the code to save changes etc.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("Are you sure you want to close the workbook?", vbYesNo, "User Input") = vbYes Then
If MsgBox("Are you relly sure you want to close the workbook?", vbYesNo, "User Input") = vbYes Then
Call MsgBox("No turning back now", vbOKOnly, "User Anwser")
End If
Else
Call MsgBox("That was close", vbOKOnly, "User Anwser")
End If
End Sub
This second set of code goes in a normal module and you could call it from your macro when you want the user to close the workbook. I have put in a workbook save so that changes are not lost.
Sub Close()
If MsgBox("Are you sure you want to close the workbook?", vbYesNo, "User Input") = vbYes Then
If MsgBox("Are you relly sure you want to close the workbook?", vbYesNo, "User Input") = vbYes Then
Call MsgBox("No turning back now", vbOKOnly, "User Anwser")
ActiveWorkbook.Close True
End If
Else
Call MsgBox("That was close", vbOKOnly, "User Anwser")
End If
End Sub
How it looks running,
Related
I have created a VBA and have tried to add a MsgBox to confirm I do want to continue. The MsgBox appears but does not respond if I click OK or X.I was hoping to be given a Yes/No choice.
Sub Clear_sheet()
ActiveSheet.Unprotect
Dim AnswerYes As String
Dim AnswerNo As String
AnswerYes = MsgBox("Are you sure?", vbQuestion + YesNo, "User Response")
If AnswerYes = vbYes Then
Range("T32 , AB32").Select
Selection.ClearContents
Range("B4:B32").Select
Selection.ClearContents
Range("W11").Select
Else
End If
ActiveSheet.Protect
End Sub
My code also has many more ranges to ClearContents. I'm wondering if the VBA will be improved with fewer lines with the ranges separated with a comma?
Many thanks for your time.
I have found the answer I needed on Google
ActiveSheet.Unprotect "Password here"
Dim AnswerYes As String
Dim AnswerNo As String
AnswerYes = MsgBox("Do you wish to continue?", vbQuestion + vbYesNo, "User Repsonse")
If AnswerYes = vbYes Then
Code to action entered here
Else
End If
ActiveSheet.Protect "Password here"
End Sub
I currently have a userform that has 3 options on printing. I want to be able to click a button, a msgbox appear with the options "yes" or "no" and if the user selects "yes", then run a sub program that's on sheet 4 (i have 2 sheets that are sheet4 and sheet2).I think the problem with the code right now is with "call slab", VBA inst recognizing the sub program "slab", this is the program that will print out my selected data.
Private Sub CommandButton1_Click()
If MsgBox("Do you want to continue?" & vbCrLf, vbYesNo) = vbYes Then
Call Slab
Else
docmd.Close commandButton_Click
End Sub
Declare the slab as public and it will be ok:
Private Sub CommandButton1_Click()
Select Case MsgBox("Do you want to continue?" & vbCrLf, vbYesNo)
Case vbYes
Slab
Case vbNo
'nothing
End Select
End Sub
Public Sub Slab()
MsgBox "Here is the SLAB"
End Sub
This question already has answers here:
Disable Excel save option but allow macro save
(2 answers)
Closed 5 years ago.
I am writing a code that will prevent the user from saving the workbook, and it will only save when I want it to. This is to prevent the user from making changes and saving when they are not supposed to. I have created two private subs, but I don't know how to make an exception when the workbook is being saved on my own. I would like to be able to place the saving code in various macros so that I can control the save at any point.
The following is my code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "You can't save this workbook!"
Cancel = True
End Sub
Private Sub Workbook_Open()
Dim myValue As String
Dim Answer As String
Dim MyNote As String
MsgBox "Welcome to the Lot Input Program"
If Range("A1").Value = "" Then
Line:
myValue = InputBox("Please input your email address:", "Input", "x#us.tel.com")
'Place your text here
MyNote = "Is this correct?: " & myValue
'Display MessageBox
Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Confirmation")
If Answer = vbNo Then
'Code for No button Press
GoTo Line
Else
Range("A1").Value = myValue
End If
ActiveWorkbook.Save
End If
End Sub
You may try something like this...
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Environ("UserName") <> "YourUserNameHere" Then
MsgBox "You can't save this workbook!"
Cancel = True
End If
End Sub
Edit:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim Ans As VbMsgBoxResult
Ans = MsgBox("You can't save this workbook!" & vbNewLine & _
"Do you have password to save the file?", vbQuestion + vbYesNo)
If Ans = vbYes Then
frmPassword.Show 'UserForm to accept the password
Else
Cancel = True
End If
End Sub
I added a public variable saveLock that I reference in the save cancel code. This allows me to lock and unlock the save inside of my code. If anyone has a better way please let me know, but this did solve the problem.
Public saveLock As Integer
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If saveLock = 0 Then
Cancel = True
End If
End Sub
Private Sub Workbook_Open()
Dim myValue As String
Dim Answer As String
Dim MyNote As String
saveLock = 0
MsgBox "Welcome to the Lot Input Program"
If Range("A1").Value = "" Then
Line:
myValue = InputBox("Please input your email address:", "Input", "x#us.tel.com")
'Place your text here
MyNote = "Is this correct?: " & myValue
'Display MessageBox
Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Confirmation")
If Answer = vbNo Then
'Code for No button Press
GoTo Line
Else
Range("A1").Value = myValue
End If
saveLock = 1
ActiveWorkbook.Save
saveLock = 0
End If
End Sub
need help with the yes no portion of the error message, if yes I want the code to be launched again and if no then exit sub.
Public Sub Reset()
Dim pt As PivotTable
Dim slice As Slicer
Application.ScreenUpdating = False
ActiveWorkbook.Model.Refresh
For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
For Each slice In pt.Slicers
slice.SlicerCache.ClearAllFilters
On Error GoTo 0
Next slice
pt.PivotCache.Refresh
Next pt
Error 0:
MsgBox "Sorry, Missing data, do you wish to continue?", _
vbCritical vbYesNo, "Restart process!"
Select Case vbYesNo
Case yes
MergeMultipleSheets
Case Else
Exit Sub
End Select
Application.ScreenUpdating = True
End Sub
Here is yes no portion of your code (assuming that you want to launch module MergeMultipleSheets if yes button is clicked):
Sub Reset()
Dim xlAns As Integer
xlAns = MsgBox("Sorry, Missing data, do you wish to continue?", vbYesNo, "Restart process!")
Select Case xlAns
Case vbYes
' do something
' if You want to call sub MergeMultipleSheets
MergeMultipleSheets
Case Else
' do something
Exit Sub
End Select
End Sub
Quick Example
Sub MsgBx()
Dim xlMsgBox As Integer
Dim Cancel As Boolean
xlMsgBox = MsgBox("Do you want to do this ", vbYesNoCancel)
If xlMsgBox = vbCancel Then
Cancel = True ' Exit
Exit Sub
ElseIf xlMsgBox = vbYes Then
' do something
ElseIf xlMsgBox = vbNo Then
' do something
End If
End Sub
I made a button (not an activeX button) and assigned a macro that is coded to kill the EXCEL.EXE task. When activated it gives me an error message saying it cannot close. Is it even possible to kill the excel task through excel?
Here's my code. I'm positive it works.
Dim oShell : Set oShell = CreateObject("WScript.Shell")
intAnswer = _
MsgBox("Do you want to close Excel?", _
vbYesNo, "Prompt")
If intAnswer = vbYes Then
MsgBox "Excel will close."
Else
MsgBox "Excel will remain open."
End If
oShell.Run "taskkill /im EXCEL.EXE", , True
Use the Application.Quit method :
intAnswer = _
MsgBox("Do you want to close Excel?", _
vbYesNo, "Prompt")
If intAnswer = vbYes Then
MsgBox "Excel will close."
Application.Quit
Else
MsgBox "Excel will remain open."
End If
Consider to use as well
ThisWorkbook.Save
if you want to save the work before closing the application. You can find out more about the Application object here.
Maybe try adding this up front:
Application.DisplayAlerts=False
ThisWorkbook.Save