2
votes

I am using VBA to check a value of a cell and call an email module to email if the cell's value is more than a value.

I want to check multiple cells but understand that it is not possible to have two Private Sub Worksheet_Change in VBA. What is the best way to check multiple cells?

Here is the code I am using;

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Application.Intersect(Range("A1"), Target) Is Nothing Then
        If IsNumeric(Target.Value) And Target.Value > 10 Then
        Call Mail_small_Text_Outlook
        End If
    End If
End Sub

Here is another if possible I would like to combine into the one Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Application.Intersect(Range("B1"), Target) Is Nothing Then
        If IsNumeric(Target.Value) And Target.Value > 20 Then
        Call Mail_small_Text_Outlook
        End If
    End If
End Sub
2
Why not put both If Not Application.Intersect... blocks into the same Sub, one after the other? - andy holaday

2 Answers

0
votes

How about doing this?

Private Sub Worksheet_Change(ByVal Target As Range)
    Call MailAlert(Target, "A1", 10)
    Call MailAlert(Target, "B1", 20)
End Sub

Private Sub MailAlert(ByVal Target As Range, ByVal Address As String, ByVal Value As Integer)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Application.Intersect(Range(Address), Target) Is Nothing Then
        If IsNumeric(Target.Value) And Target.Value > Value Then
        Call Mail_small_Text_Outlook
        End If
    End If
End Sub
0
votes
Private Sub Worksheet_Change(ByVal Target As Range)

Select Case Taget.Address

  Case "$A$1" 'This will make sure its just one cell and its A1          
      If IsNumeric(Target.Value) And Target.Value > 10 Then         
        Call Mail_small_Text_Outlook         
      End If     

  Case "$B$1" 'This will make sure its just one cell and its B1
      If IsNumeric(Target.Value) And Target.Value > 20 Then         
        Call Mail_small_Text_Outlook         
      End If 

  'Case ... whatever else you want.

End Select
End Sub

There may be more efficient ways, but this is what first came to mind. Hope this answers your question.