How to stop event-based VBA code from starting when an error occurs in a spreadsheet?

I have a code for automatically sending email when a cell reaches a certain value. (Below)

If an error occurs and the cell has been deleted, it also sends an email. What do I need to add to the code to stop sending it if I remove the value from the cell?

Thanks in advance.

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub

If Not Application.Intersect(Range("M4:M368"), Target) Is Nothing Then
    If IsNumeric(Target.Value) And Target.Value < 1000 Then
        Call Fuel_LevelW01D
    End If
End If
End Sub


+4
source share
1 answer
  • You do not need to test IsNumeric
  • It’s best to run two separate ones IF's, and not AND, because for <1000no point testing, if the cell does not matter

updated code

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Application.Intersect(Range("M4:M368"), Target) Is Nothing Then
    If Len(Target.Value) > 0 Then
        If Target.Value < 1000 Then Call Fuel_LevelW01D
    End If
End If
End Sub
+3
source

All Articles