Home » excel » excel – Cancel = True is not working in Workbook_BeforeClose

excel – Cancel = True is not working in Workbook_BeforeClose

Posted by: admin May 14, 2020 Leave a comment

Questions:

Debug Mode, showing that value of <code>Cancel</code> is set to <code>True</code>

This clearly shows that value of cancel is true, and still my workbook closes after this! Why?

I want to cancel closing of the excel file if user input is incorrect.

I debug the code and found out that the value of Cancel was initially False, and it becomes True. But once the Sub ends, file is still closed.

(variable error is defined at workbook declaration space, so accessible within module.)

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        error = 0

        check_for_error
        If error = 1 Then
            Cancel = True
            Exit Sub
        End If

        Application.EnableEvents = False
        Sheet1.Cells.ClearContents
        Sheet2.Cells.Clear
        Application.EnableEvents = True
    End Sub

I want to cancel closing of file if error = 1.

Update:: Thanks everyone for the responses, but nothing seems to work for me! Following is how my current code looks.

Option Explicit
Dim errYes As Byte

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    check_freeze_panel
    If errYes = 1 Then
        Cancel = True
        Exit Sub
    End If

    Application.DisplayAlerts = False
    ThisWorkbook.Save
    Application.DisplayAlerts = True
End Sub

errYes is returning 1, Cancel = True is being executed, and still my workbook closes.

I even tried commenting all the code, and just putting Cancel = True as suggested and guess what, it still closes!

How to&Answers:

Change the name of error this is a reserved word for an already existing function (see Error function) and must not be used as a variable name. And activate Option Explicit and declare all your variables.

Here is an example how you would do it correctly:

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim ErrorsFound As Boolean
    ErrorsFound = check_for_error

    If ErrorsFound Then
        Cancel = True
        Exit Sub
    End If

    Application.EnableEvents = False
    Sheet1.Cells.ClearContents
    Sheet2.Cells.Clear
    Application.EnableEvents = True
End Sub

Function check_for_error() As Boolean
    'check and return
    check_for_error = True 'errors found
End Function

Or even shorter:

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Cancel = check_for_error

    If Not Cancel Then
        Application.EnableEvents = False
        Sheet1.Cells.ClearContents
        Sheet2.Cells.Clear
        Application.EnableEvents = True
    End If
End Sub

Function check_for_error() As Boolean
    'check and return
    check_for_error = True 'errors found
End Function

Answer:

Try this. Set Error equal to your function call and make sure it returns the right data type.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    error = check_for_error

    If error = 1 Then
        Cancel = True
        Exit Sub
    End If

    Application.EnableEvents = False
    Sheet1.Cells.ClearContents
    Sheet2.Cells.Clear
    Application.EnableEvents = True
End Sub

Answer:

Initially Cancel is equal False, means File Close will succeed.
For me the below code works fine. When I set error = 1 then closing is cancelled whether you use “Exit Sub” or not. When I set error <> 1 then cls
When I test your Code:

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim error As Integer
error = 1

If error = 1 Then
    Cancel = True

    'Exit Sub
End If

End Sub

Just test this:
Write False and workbook should Close, and True should “stop” the closing.

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Cancel = False

End Sub