EnableEvents = True does not work

RomulusMilea

Board Regular
Joined
May 11, 2002
Messages
181
Hello all,

I use Excel 2007. I have got the entire code below, meant to force the user to open the file with macros enabled (we prefer to use this option) and also meant to ask the user for number of appraisers, before doing anything to the file.

I have put Application.EnableEvents back to True (see the line below, marked in bold red), but if the user presses Cancel button "during" procedure "Set_no_of_appraisers" (see below), the EnableEvents stays on False.

This is confirmed also by Excel Immediate Window, it says "False" :(.

I have tried to solve this by using others experience all over the Internet, but no thread/topic/forum explains this simply and clearly to a VBA novice as I am :).

When is the most appropriate to set EnableEvents to True ?

Where am I going wrong ?

Please provide an answer, based on the code below. Thank you so much.

Rich (BB code):
Option Explicit
Const WelcomePage = "Enable macros"
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False
'Evaluate if workbook is saved and emulate default propmts
With ThisWorkbook
If Not .Saved Then
Select Case MsgBox("Do you want to save the BLOODY changes you made to '" & .Name & "'?", _
vbYesNoCancel + vbExclamation)
Case Is = vbYes
'Call customized save routine
Call CustomSave
Case Is = vbNo
'Do not save
Case Is = vbCancel
'Set up procedure to cancel close
Cancel = True
End Select
End If
'If Cancel was clicked, turn events back on and cancel close,
'otherwise close the workbook without saving further changes
If Not Cancel = True Then
.Saved = True
Application.EnableEvents = True
.Close SaveChanges:=False
Else
Application.EnableEvents = True
End If
End With
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False
'Call customized save routine and set workbook's saved property to true
'(To cancel regular saving)
Call CustomSave(SaveAsUI)
Cancel = True
'Turn events back on an set saved property to true
Application.EnableEvents = True
ThisWorkbook.Saved = True
End Sub
Private Sub Workbook_Open()
'Unhide all worksheets
Application.ScreenUpdating = False
Call ShowAllSheets
Application.ScreenUpdating = True
Set_no_of_appraisers
End Sub
Private Sub CustomSave(Optional SaveAs As Boolean)
Dim ws As Worksheet, aWs As Worksheet, newFname As String
'Turn off screen flashing
Application.ScreenUpdating = False
'Record active worksheet
Set aWs = ActiveSheet
'Hide all sheets
Call HideAllSheets
'Save workbook directly or prompt for saveas filename
If SaveAs = True Then
newFname = Application.GetSaveAsFilename( _
fileFilter:="Excel Files (*.xls), *.xls")
If Not newFname = "False" Then ThisWorkbook.SaveAs newFname
Else
ThisWorkbook.Save
'On Error Resume Next
End If
'Restore file to where user was
Call ShowAllSheets
aWs.Activate
'Restore screen updates
Application.ScreenUpdating = True
End Sub
Private Sub HideAllSheets()
'Hide all worksheets except the macro welcome page
Dim ws As Worksheet
Worksheets(WelcomePage).Visible = xlSheetVisible
For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVeryHidden
Next ws
Worksheets(WelcomePage).Activate
End Sub
Private Sub ShowAllSheets()
'Show all worksheets except the macro welcome page
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVisible
Next ws
Worksheets(WelcomePage).Visible = xlSheetVeryHidden
End Sub
 
Private Sub Set_no_of_appraisers()
'Created by Romulus Milea, Romulus.Milea@GMail.com;
'Version 1.0, latest update: 1/Sep/2011;
'Code history:
'Version 1.0  1/Sep/2011 First issue
Application.EnableEvents = False
Dim InputBoxString As String
Dim ValidEntry As Boolean
Dim UserEntry As Variant
Worksheets("Gage R&R Continuous (Rows)").Range("B14").ClearContents
InputBoxString = "Please enter number of APPRAISERS (2 or 3):"
ValidEntry = False
Do
    UserEntry = InputBox(InputBoxString)
    If IsNumeric(UserEntry) Then
        If (UserEntry = 2) Or (UserEntry = 3) Then
            ValidEntry = True
        Else
            InputBoxString = "Your previous numeric entry was INVALID."
            InputBoxString = InputBoxString & vbCrLf & vbCrLf
            InputBoxString = InputBoxString & "You must enter 2, or 3. Please re-try !"
        End If
    Else
        InputBoxString = "Your previous entry was NOT numeric, therefore is it INVALID."
        InputBoxString = InputBoxString & vbCrLf & vbCrLf
        InputBoxString = InputBoxString & "You must enter 2, or 3. Please re-try !"
    End If
    If UserEntry = "" Then
            MsgBox "You entered no value, or you cancelled the entry, therefore active file will be now closed, without being saved."
            ActiveWorkbook.Close SaveChanges:=False
            Application.EnableEvents = True
    End If
Loop Until ValidEntry = True
 
Worksheets("Gage R&R Continuous (Rows)").Range("B14").Value = Val(UserEntry)
Cells(1, 1).Select
MsgBox "Number of appraisers is now set to " & UserEntry & " (see cell B15). " & _
"If you need to change this value you must close the file and open it again."
End Sub
 
Hello MikeErickson,

Thank you for the explanations and improved procedure. I have used it, but unfortunately after I press "Cancel", Application.EnableEvents remains blocked on False.

The Set_no_of_appraisers procedure seems to work correctly, I guess the cause of the issue is somewhere else.

Do you have any idea ? Thank you again.

Here is the code I used:

Code:
Private Sub Set_no_of_appraisers()
 
    Dim InputBoxString As String
    Dim UserEntry As Variant
 
    Application.EnableEvents = False
 
    Worksheets("Gage R&R Continuous (Rows)").Range("B15").ClearContents
    InputBoxString = "Please enter number of APPRAISERS (2 or 3):"
    Do
        UserEntry = InputBox(InputBoxString)
        InputBoxString = "Value you entered is NOT correct." & vbCr & vbCr & "Please enter number of APPRAISERS (2 or 3):"
    Loop Until (UserEntry <> vbNullString) Imp (UserEntry = "2" Or UserEntry = "3")
 
    If UserEntry = vbNullString Then
        MsgBox "You entered no value, or you cancelled the entry, therefore active file will be now closed, without being saved."
        ActiveWorkbook.Close SaveChanges:=False
    Else
        Worksheets("Gage R&R Continuous (Rows)").Range("B15").Value = Val(UserEntry)
        Cells(1, 1).Select
        MsgBox "Number of appraisers is now set to " & UserEntry & " (see cell B15). " & _
            "If you need to change this value you must close the file and open it again."
    End If
 
    Application.EnableEvents = True
 
End Sub
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try this:-
Code:
Private Sub Set_no_of_appraisers()
 
    Dim InputBoxString As String
    Dim UserEntry As Variant
 
    Application.EnableEvents = False
 
    Worksheets("Gage R&R Continuous (Rows)").Range("B15").ClearContents
    InputBoxString = "Please enter number of APPRAISERS (2 or 3):"
    Do
        UserEntry = InputBox(InputBoxString)
        InputBoxString = "Value you entered is NOT correct." & vbCr & vbCr & "Please enter number of APPRAISERS (2 or 3):"
    Loop Until (UserEntry <> vbNullString) Imp (UserEntry = "2" Or UserEntry = "3")
 
    If UserEntry = vbNullString Then
        MsgBox "You entered no value, or you cancelled the entry, therefore active file will be now closed, without being saved."
        Application.EnableEvents = True
        ActiveWorkbook.Close SaveChanges:=False
    Else
        Worksheets("Gage R&R Continuous (Rows)").Range("B15").Value = Val(UserEntry)
        Cells(1, 1).Select
        MsgBox "Number of appraisers is now set to " & UserEntry & " (see cell B15). " & _
            "If you need to change this value you must close the file and open it again."
    End If
 
    Application.EnableEvents = True
 
End Sub
 
Upvote 0
Hello Richard,

Thank you for your suggestion, I used it in the past, but it did not work. If we force EnableEvents to True right before file closure, it will actually trigger Workbook_BeforeClose procedure (see entire code, inserted below).

What I want to achieve is: if the user presses "Cancel" button, Excel must close the file without saving and without asking the user whether the file should be saved, or not.

I guess the issue is not on Set_no_of_appraisers procedure (it works just fine), it could be somewhere else.

I do not why Application.EnableEvents is blocked to False, that is the issue.

Any other idea ? Thank you for your effort.

Entire code stored on ThisWorkbook module:

Rich (BB code):
Option Explicit
 
Const WelcomePage = "Enable macros"
 
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
'Turn off events to prevent unwanted loops
 
Application.EnableEvents = False
 
'Evaluate if workbook is saved and emulate default propmts
 
With ThisWorkbook
 
If Not .Saved Then
Select Case MsgBox("Do you want to save the BLOODY changes you made to '" & .Name & "'?", _
vbYesNoCancel + vbExclamation)
 
Case Is = vbYes
'Call customized save routine
Call CustomSave
 
Case Is = vbNo
'Do not save
Case Is = vbCancel
 
'Set up procedure to cancel close
Cancel = True
 
End Select
End If
 
'If Cancel was clicked, turn events back on and cancel close,
'otherwise close the workbook without saving further changes
 
If Not Cancel = True Then
.Saved = True
 
Application.EnableEvents = True
.Close SaveChanges:=False
 
Else
 
Application.EnableEvents = True
 
End If
 
End With
End Sub
 
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
 
'Turn off events to prevent unwanted loops
 
Application.EnableEvents = False
 
'Call customized save routine and set workbook's saved property to true
'(To cancel regular saving)
 
Call CustomSave(SaveAsUI)
 
Cancel = True
 
'Turn events back on an set saved property to true
Application.EnableEvents = True
 
ThisWorkbook.Saved = True
 
End Sub
 
Private Sub Workbook_Open()
 
'Unhide all worksheets
Application.ScreenUpdating = False
 
Call ShowAllSheets
 
Application.ScreenUpdating = True
 
Set_no_of_appraisers
 
End Sub
 
Private Sub CustomSave(Optional SaveAs As Boolean)
 
Dim ws As Worksheet, aWs As Worksheet, newFname As String
 
'Turn off screen flashing
Application.ScreenUpdating = False
 
'Record active worksheet
Set aWs = ActiveSheet
 
'Hide all sheets
Call HideAllSheets
 
'Save workbook directly or prompt for saveas filename
If SaveAs = True Then
 
newFname = Application.GetSaveAsFilename( _
fileFilter:="Excel Files (*.xls), *.xls")
 
If Not newFname = "False" Then ThisWorkbook.SaveAs newFname
 
Else
 
ThisWorkbook.Save
'On Error Resume Next
 
End If
 
'Restore file to where user was
Call ShowAllSheets
 
aWs.Activate
'
Restore screen updates
Application.ScreenUpdating = True
 
End Sub
 
Private Sub HideAllSheets()
'Hide all worksheets except the macro welcome page
 
Dim ws As Worksheet
 
Worksheets(WelcomePage).Visible = xlSheetVisible
 
For Each ws In ThisWorkbook.Worksheets
 
If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVeryHidden
Next ws
 
Worksheets(WelcomePage).Activate
 
End Sub
 
Private Sub ShowAllSheets()
'Show all worksheets except the macro welcome page
 
Dim ws As Worksheet
 
For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVisible
Next ws
 
Worksheets(WelcomePage).Visible = xlSheetVeryHidden
 
End Sub
 
Private Sub Set_no_of_appraisers()
 
    Dim InputBoxString As String
    Dim UserEntry As Variant
    Application.EnableEvents = False
 
    Worksheets("Gage R&R Continuous (Rows)").Range("B15").ClearContents
    InputBoxString = "Please enter number of APPRAISERS (2 or 3):"
    Do
        UserEntry = InputBox(InputBoxString)
        InputBoxString = "Value you entered is NOT correct." & vbCr & vbCr & "Please enter number of APPRAISERS (2 or 3):"
    Loop Until (UserEntry <> vbNullString) Imp (UserEntry = "2" Or UserEntry = "3")
 
    If UserEntry = vbNullString Then
        MsgBox "You entered no value, or you cancelled the entry, therefore active file will be now closed, without being saved."
        ActiveWorkbook.Close SaveChanges:=False
    Else
        Worksheets("Gage R&R Continuous (Rows)").Range("B15").Value = Val(UserEntry)
        Cells(1, 1).Select
        MsgBox "Number of appraisers is now set to " & UserEntry & " (see cell B15). " & _
            "If you need to change this value you must close the file and open it again."
    End If
 
    Application.EnableEvents = True
End Sub
 
Upvote 0
Try this slight modification:-
Code:
Private Sub Set_no_of_appraisers()
 
    Dim InputBoxString As String
    Dim UserEntry As Variant
 
    Application.EnableEvents = False
 
    Worksheets("Gage R&R Continuous (Rows)").Range("B15").ClearContents
    InputBoxString = "Please enter number of APPRAISERS (2 or 3):"
    Do
        UserEntry = InputBox(InputBoxString)
        InputBoxString = "Value you entered is NOT correct." & vbCr & vbCr & "Please enter number of APPRAISERS (2 or 3):"
    Loop Until (UserEntry <> vbNullString) Imp (UserEntry = "2" Or UserEntry = "3")
 
    If UserEntry = vbNullString Then
        MsgBox "You entered no value, or you cancelled the entry, therefore active file will be now closed, without being saved."
        Application.EnableEvents = True
        thisworkbook.saved=true
        ActiveWorkbook.Close SaveChanges:=False
    Else
        Worksheets("Gage R&R Continuous (Rows)").Range("B15").Value = Val(UserEntry)
        Cells(1, 1).Select
        MsgBox "Number of appraisers is now set to " & UserEntry & " (see cell B15). " & _
            "If you need to change this value you must close the file and open it again."
    End If
 
    Application.EnableEvents = True
 
End Sub
 
Upvote 0
Hi Richard,

I have just tried the latest version you suggested, and... IT WORKS !!! I have run it several times, Application.EnableEvents was always set on True.

Great, thank you for your time. Have a spendid day.

Topic could be considered closed, from my point of view. If other issues will pop up, I will open another post, or I will re-open this one.
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,904
Members
452,948
Latest member
Dupuhini

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