Replace the old code with the following
Public Flg As Boolean Private Sub Workbook_BeforeClose(Cancel As Boolean) Call Sample If Flg Then Cancel = True End Sub Private Sub Sample() Dim r As Long, a, i As Long, ws As Worksheet Set ws = Sheets("Sheet1") a = ws.Range("a1").CurrentRegion Flg = False For i = 1 To UBound(a, 2) With Application If .CountA(.Index(a, 0, i)) <> UBound(a, 1) Then Flg = True MsgBox "Please enter all mandatory fields!" ws.Activate Exit Sub End If End With Next End Sub