2012-08-01 47 views
1

我有當前的代碼計算單個單元格的值,然後調用一個模塊,如果它的值超過另一個值。Worksheet_Calculate在多個範圍

如何讓它檢查單元格的多個範圍,例如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 

回答

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 
+0

第一個例子就是我以前的樣子。謝謝! – 2012-08-01 07:27:34

+0

嗨Siddarth,這工作正常,但它會在每次輸入數據時計算整個工作表,即使輸入的新數據超出了公式計算範圍並每次發送一封電子郵件。有一種方法可以在電子郵件發送後以某種方式解除警報,從而在每次輸入數據時不會發送郵件? – 2012-08-15 22:57:30

+0

是的,您可以使用相交方法。數據是否會手動輸入這兩個範圍? – 2012-08-15 22:59:23