我四处搜索,并编写了以下代码,我只想在特定单元格 D4 更改时运行这些代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Static EmailSent As Boolean
Dim Threshold As Integer
Dim Cell As String, Email As String, Msg As String
Cell = "D4"
Threshold = 100
Email = Range("E7").Value
Set KeyCells = Range(Cell)
If Not Application.Intersect(Range(Cell), Range(Target.Address)) Is Nothing Then
Dim x As Integer
x = Range(Cell).Value
If x >= Threshold Then
EmailSent = False
ElseIf x < Threshold And Not EmailSent Then
EmailSent = True
Msg = "You only have " & x & " widgets remaining."
MsgBox Msg
SendMail Email, Msg
End If
End If
End Sub
这行得通,我知道这里有很多类似的问题。但这就是我遇到麻烦的地方:这仅在我将 D4 设置为显式值时才有效,例如"48"
. 即使 D4 是一个公式,我也希望"=SUM(A4:C4)"
它能够工作:所以如果 D4 是,那么如果总和低于 100,则应该发送一封电子邮件。在这种情况下,此代码不会发送电子邮件:-(
有谁知道如何解决这一问题?