2

我正在使用 VBA 检查单元格的值,如果单元格的值大于一个值,则调用电子邮件模块发送电子邮件。

我想检查多个单元格,但了解在 VBA 中不可能有两个 Private Sub Worksheet_Change。检查多个单元格的最佳方法是什么?

这是我正在使用的代码;

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

如果可能的话,这是另一个我想合并到一个 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
4

2 回答 2

0
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

可能有更有效的方法,但这是首先想到的。希望这能回答你的问题。

于 2012-06-28T02:51:47.787 回答
0

这样做怎么样?

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
于 2012-06-28T03:35:09.643 回答