我正在使用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
答案 0 :(得分: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
可能有更有效的方式,但这是首先想到的。希望这能回答你的问题。
答案 1 :(得分: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