![](/img/trans.png)
[英]How Does One Combine two Private Sub Worksheet_Change(ByVal Target As Range) in One Work Sheet?
[英]Difficulty merging 2 x Private Sub Worksheet_Change (ByVal Target As Range) in one Excel sheet
我已經成功地編寫了兩個宏,用於基於工作表中的單元格值自動進行電子郵件發送(基本上作為提醒系統)。 范圍重疊,一個子單元旨在當單元格的值為0時發送電子郵件,另一個子單元格較小的單元格范圍,並且當單元格的報告范圍在1-5之間(包括端值在內)時,用於發送電子郵件)。
我可以讓潛艇單獨工作沒問題,但是當我試圖將兩者合並時,我的知識非常有限。 要么根本不起作用,要么只能部分起作用。
如果有人可以幫助我,我將非常感謝,因為我很茫然! 這兩個子代碼如下:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Application.Intersect(Range("D122:D128,D131:D133,D138,D140,D144,D188,D191:D192,D217:D220,D294,D159:D167"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value > 0 Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
zRow = Target.Row
zValno = Cells(zRow, "B")
zValname = Cells(zRow, "C")
zValInno = Cells(zRow, "D")
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = ""
strbody = strbody & "Please be advised that " & zValno & " (" & zValname & ") " & "is now low. This value is now " & zValInno & "."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
On Error Resume Next
With OutMail
.to = "abc@anyoldemail.com"
.CC = ""
.BCC = ""
.Subject = "LOW VALUE: " & zValno & " is now low."
.Body = strbody
.Attachments.Add ("C:\reportlog.txt")
.Send
End With
On Error GoTo 0
zSent = zSent + 1
saywhat = "processing " & zSent & " of " & zCount
Application.StatusBar = saywhwat
Application.StatusBar = ""
Set OutMail = Nothing
Set OutApp = Nothing
End If
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("D4:D100,G4:G100,J4:J99"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 1 Then
zRow = Target.Row
zValno = Cells(zRow, "B")
zValname = Cells(zRow, "C")
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = ""
strbody = strbody & "Please be advised that " & zValno & " (" & zValname & ") " & "is now reporting nil."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
On Error Resume Next
With OutMail
.to = "abc@anyoldemail.com"
.CC = ""
.BCC = ""
.Subject = "NULL ALERT: " & zValno & " is now reporting nil."
.Body = strbody
.Attachments.Add ("C:\reportlog.txt")
.Send
End With
On Error GoTo 0
zSent = zSent + 1
saywhat = "processing " & zSent & " of " & zCount
Application.StatusBar = saywhwat
Application.StatusBar = ""
Set OutMail = Nothing
Set OutApp = Nothing
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("D122:D128,D131:D133,D138,D140,D144,D188,D191:D192,D217:D220,D294,D159:D167"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value > 0 Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
zRow = Target.Row
zValno = Cells(zRow, "B")
zValname = Cells(zRow, "C")
zValInno = Cells(zRow, "D")
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = ""
strbody = strbody & "Please be advised that " & zValno & " (" & zValname & ") " & "is now low. This value is now " & zValInno & "."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
On Error Resume Next
With OutMail
.to = "abc@anyoldemail.com"
.CC = ""
.BCC = ""
.Subject = "LOW VALUE: " & zValno & " is now low."
.Body = strbody
.Attachments.Add ("C:\reportlog.txt")
.Send
End With
End If
End If
ElseIf Not Application.Intersect(Range("D4:D100,G4:G100,J4:J99"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 1 Then
zRow = Target.Row
zValno = Cells(zRow, "B")
zValname = Cells(zRow, "C")
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = ""
strbody = strbody & "Please be advised that " & zValno & " (" & zValname & ") " & "is now reporting nil."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
On Error Resume Next
With OutMail
.to = "abc@anyoldemail.com"
.CC = ""
.BCC = ""
.Subject = "NULL ALERT: " & zValno & " is now reporting nil."
.Body = strbody
.Attachments.Add ("C:\reportlog.txt")
.Send
End With
End If
End If
On Error GoTo 0
zSent = zSent + 1
saywhat = "processing " & zSent & " of " & zCount
Application.StatusBar = saywhwat
Application.StatusBar = ""
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
進行一些修改后,嘗試對兩個Worksheet_Change
事件使用下面的組合代碼。
我添加了一個變量EmailType
,用於檢查修改后的單元格是否通過了2個條件之一,然后得到的值是1或2。
然后,根據EmailType
修改電子郵件參數。
碼
Private Sub Worksheet_Change(ByVal Target As Range)
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim mailSubject As String '<-- added this String variable to differ on 2 scenarios
Dim EmailType As Long '<-- use variable to see if passed the 2 criterias in the original code
EmailType = 0 '<-- init value
If Target.Cells.Count > 1 Then Exit Sub
If Not Application.Intersect(Range("D122:D128,D131:D133,D138,D140,D144,D188,D191:D192,D217:D220,D294,D159:D167"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value > 0 And Target.Value < 6 Then
EmailType = 1 '<-- Email Type = 1
End If
End If
If Not Application.Intersect(Range("D4:D100,G4:G100,J4:J99"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 1 Then
EmailType = 2 '<-- Email Type = 2
End If
End If
If EmailType = 0 Then Exit Sub '< didn't pass any of the criterias >> Exit Sub
zValno = Range("B" & Target.Row)
zValname = Range("C" & Target.Row)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Select Case EmailType
Case 1
zValInno = Cells("D" & Target.Row) '<-- this value exists on for Email Type 1
mailSubject = "LOW VALUE: " & zValno & " is now low." '<-- mail subject for email type 1
strbody = ""
strbody = strbody & "Please be advised that " & zValno & " (" & zValname & ") " & "is now low. This value is now " & zValInno & "."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
Case 2
mailSubject = "NULL ALERT: " & zValno & " is now reporting nil." '<-- mail subject for email type 2
strbody = ""
strbody = strbody & "Please be advised that " & zValno & " (" & zValname & ") " & "is now reporting nil."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
strbody = strbody & vbCr & vbCr
strbody = strbody & "Blah, blah, blah."
End Select
' ======= from here untill the end the same code, just using different values found per Email Type =======
On Error Resume Next
With OutMail
.to = "abc@anyoldemail.com"
.CC = ""
.BCC = ""
.Subject = mailSubject
.Body = strbody
.Attachments.Add ("C:\reportlog.txt")
.Send
End With
On Error GoTo 0
zSent = zSent + 1
saywhat = "processing " & zSent & " of " & zCount
Application.StatusBar = saywhat
Application.StatusBar = ""
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.