当公式单元格提供两种文本时触发宏

trigger macro when a formula cell gives two kind of text

我想创建一个宏,当包含公式的特定单元格范围为我们提供两个特定字符串作为输出时调用另一个宏。公式是=IF(E15<=$G,"CHECK",IF(E15<=$F,"WARNING","OK")),我想在每次输出“CHECK”and/or“WARNING”时触发宏。我想在每次输出给出这两个字符串时触发宏,而不仅仅是在此范围内一次。我要查看并包含上述公式的触发范围是F8:F38。我可以找到这样的东西

   If Range("F8:F38").Value = "CHECK" Then
   Call email
End Sub

但如果有公式而不是字符串,这将不起作用。

您可以使用 Worksheet.Calculate 事件和循环:

Private Sub Worksheet_Calculate()
    Dim cell As Range
    For Each cell In Me.Range("F8:F38")
        If cell.Value = "CHECK" Or cell.Value = "WARNING" Then
            Email ' no need for Call
        End If
    Next
End Sub

如果您只想在 F3:F38 中的值 更改 email,则如下所示:

在常规模块中:

Public vals() As Variant ' public variable containing the 'old' values

ThisWorkbook模块中:

Private Sub Workbook_Open()
    ' populate vals when opening workbook
    vals = Me.Worksheets("Yourworksheetname").Range("F3:F38").Value
End Sub

在sheet代码模块中:

Private Sub Worksheet_Calculate()
    Dim cell As Range
    For Each cell In Me.Range("F8:F38")
        Dim counter As Long
        counter = counter + 1
        
        If cell.Value = "CHECK" Or cell.Value = "WARNING" Then
            ' check if the 'new' value is different from the 'old' one
            If cell.Value <> vals(counter, 1) Then 
                Email
            End If
        End If
    Next
    
    vals = Me.Range("F8:F38").Value ' store the 'new' values 
End Sub