当电子表格中出现错误时,如何停止触发基于事件的VBA代码?

时间:2022-11-13 20:57:14

I have a set up a code to automatically send an email when a cell reaches a certain value. (below)

当一个单元格达到某个值时,我设置了一个自动发送电子邮件的代码。(下图)

If an error is made and the cell is then deleted, it sends the email as well. What do i need to add to the code to stop it sending if i delete 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


1 个解决方案

#1


3  

  • You don't need to test for IsNumeric
  • 不需要对IsNumeric进行测试
  • Best to run two separate IF's rather than an AND as there is no point testing for <1000 unless the cell has a value
  • 最好运行两个独立的IF's而不是an,因为对于 <没有点测试,除非单元格有一个值< li>

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

#1


3  

  • You don't need to test for IsNumeric
  • 不需要对IsNumeric进行测试
  • Best to run two separate IF's rather than an AND as there is no point testing for <1000 unless the cell has a value
  • 最好运行两个独立的IF's而不是an,因为对于 <没有点测试,除非单元格有一个值< li>

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