(Solved) – problem with the Cancel event on BeforeClose


I have a problem with the Cancel event in the following procedure, with which I cannot cancel the closing of the book. Although I set the value of Cancel to True, and have verified that it takes it properly, the book still closes. Can you help me identify what the problem is?

Here is the code:

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim xx As Integer, difFecha As Double, MemoMes As String, MemoNuMes As String
Dim TSel As Integer

MemoMes = "": MemoNuMes = ""

For xx = 23 To 34
difFecha = DateDiff("s", Sheets("PARAM").Cells(xx, 8), Sheets("PARAM").Cells(xx, 9))
If difFecha < 0 Then
MemoMes = MemoMes & Sheets("PARAM").Cells(xx, 7) & " " ' hay modificaciones sin calcular
If MemoNuMes = "" Then MemoNuMes = Format(xx - 22, "00")
End If
Next xx

If MemoMes <> "" Then ' hay modificaciones sin calcular
TSel = MsgBox("En la(s) hoja(s) del mes " & MemoMes & "parece que ha hecho modificaciones y NO ha CALCULADO." & Chr(10) & _
"¿Quiere calcular antes de cerrar?", vbYesNo   vbExclamation, "ATENCIÓN")
If TSel = vbYes Then
Cancel = True
Exit Sub
End If
End If

End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *