Private Sub Worksheet_Deactivate()
Dim wsMove As Worksheet
Dim rngColsCheck As Range
Dim rngSglCol As Range
Dim strColNames As String
'MsgBox Variables
Dim strMsg As String
Dim mbOpts As Long
Dim strTitle As String
Set wsMove = ActiveSheet
Me.Activate
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Specify the columns to check for positive values here
Set rngColsCheck = Me.Range("C:C, D:D, H:H")
For Each rngSglCol In rngColsCheck.Columns
If WorksheetFunction.CountIf(rngSglCol, ">0") > 0 Then
'Build a list of columns that still contain positive values
strColNames = strColNames & Left(Replace(rngSglCol.Address, "$", ""), 1) & ", "
End If
Next rngSglCol
'If no columns have a postive value clean up and then End Sub so message isn't shown
If strColNames = "" Then
wsMove.Activate
GoTo clean_up
End If
'Strip the final separator from the list of columns
strColNames = Left(strColNames, Len(strColNames) - 2)
'Set message shown
strMsg = "There are positive values left in the columns: " & strColNames & vbCrLf & _
"Would you still like to leave the sheet?"
'Set style of MsgBox by commenting out the appropriate option below
mbOpts = vbYesNo + vbCritical 'Yes/No buttons with a Red X image (Stop Warning)
'mbOpts = vbYesNo + vbInformation 'Yes/No buttons with a blue i image (Information warning)
'Set title of message box
strTitle = "Leave Sheet?"
If MsgBox(strMsg, mbOpts, strTitle) = vbYes Then
wsMove.Activate
End If
clean_up:
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub