I have vba code which pops up
msg box if the entered value is zero. if the user later wants to change the zero to some other number it will still pops up the
msg box before it allows to edit the zero to some other number.
I don’t want the
msgbox pop up if user is changing the existing value.
Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False If Intersect(Target, Range("b4:af18")) Is Nothing Then If Target.Value = 0 Then Application.EnableEvents = True MsgBox "This is it", vbApplicationModal, "Scikess/Holiday" End If End If Application.EnableEvents = True End Sub
I believe you are describing a situation where the user taps the delete key before typing in a new answer. In that case the blank cell has a numerical value of zero and you are getting a false positive on the check to see if the user typed in a zero.
Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("b4:af18")) Is Nothing Then On Error GoTo bm_Safe_Exit Dim trgt As Range Application.EnableEvents = False For Each trgt In Intersect(Target, Range("b4:af18")) If trgt.Value = 0 And trgt.Text = "0" Then MsgBox "This is it" & Chr(10) & trgt.Address(0, 0) & " cannot be zero.", _ vbApplicationModal, "Scikess/Holiday" Application.Undo trgt.Activate Exit For End If Next trgt End If bm_Safe_Exit: Application.EnableEvents = True End Sub
I’ve attempted to have the code run when more than a single Target is involved (e.g. a paste or fill operation) but this makes the
Application.Undo difficult to keep the other valid entries. Leave a comment on this behavior if it is unsuitable.
It also wasn;t clear on what range you wanted this to react on. You seem to have missed the
Not in your Intersect method, making it react on any cell that wasn’t in the range. I’ve adjusted above to include the range you specified, not exclude it.