[英]Combine two Private Sub Worksheet_Change
我正在使用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
如果可能的話,這是另一種,我想合並到一個子中
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
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
可能有更有效的方法,但這是最先想到的。 希望這能回答您的問題。
怎么樣呢?
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.