Worksheet_Calculate在多个范围

时间:2012-08-01 06:56:22

标签: excel vba excel-vba

我有当前代码计算单个单元格的值,然后调用模块,如果它的值超过另一个值。

如何检查多个单元格范围,例如B5:E5,B8:M8,如果范围内的任何单元格超过该值,则调用模块。

Private Sub Worksheet_Calculate()
If Range("B5") > 4 Then
Application.EnableEvents = False
Application.Run "Mail_small_Text_Outlook"
Application.EnableEvents = True
End If
End Sub

1 个答案:

答案 0 :(得分:0)

这是你在尝试的吗?

Private Sub Worksheet_Calculate()
    Dim aRng As Range, bRng As Range
    Dim aCell As Range

    Set aRng = Range("B5:E5")
    Set bRng = Range("B8:M8")

    For Each aCell In aRng
        If aCell.Value > 4 Then
            '
            Application.Run "Mail_small_Text_Outlook"
            '
            Exit Sub
        End If
    Next

    For Each aCell In bRng
        If aCell.Value > 4 Then
            '
            Application.Run "Mail_small_Text_Outlook"
            '
            Exit Sub
        End If
    Next
End Sub

或类似的东西?

Private Sub Worksheet_Calculate()
    Dim aRng As Range, bRng As Range
    Dim aCell As Range, uRng As Range

    Set aRng = Range("B5:E5")
    Set bRng = Range("B8:M8")
    Set uRng = Union(aRng, bRng)

    For Each aCell In uRng
        If aCell.Value > 4 Then
            '
            Application.Run "Mail_small_Text_Outlook"
            '
            Exit Sub
        End If
    Next
End Sub