【发布时间】:2018-07-31 08:42:23
【问题描述】:
我想根据不同单元格和不同条件的条件发送电子邮件。 不幸的是,我的代码仅适用于第一个范围(“A4”到“H4”)。
如果我更改其他内容,它将不会被触发。 任何想法如何解决这个问题?
附加:我想在电子邮件中写下受影响单元格上方的 4 个单元格。 例如A4 会触发条件我想写“A2, A3”的值 B2,B3”在电子邮件中。 有人可能知道如何在受影响的单元格上方选择一个 4x4 的区域?!这是可能的还是我需要在我的代码中使用这个?!
谢谢。
顺便说一句:我知道我的代码很糟糕,但我是 VBA 的新手,所以我很高兴它能正常工作。 :D
原文代码:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim rg1, rg2, rg3, rg4, rg5, rg6, rg7, rg8, rg9, rg10 As Range
Dim rg11, rg12, rg13, rg14, rg15, rg16, rg17, rg18, rg19, rg20 As Range
Set rg1 = Intersect(Range("A4", "H4"), Target)
If rg1 Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
End If
' ... similar for all ranges (with different range and condition)
End Sub
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
If MsgBox("Senden?", vbOKCancel) = vbOK Then
xMailBody = "test" & vbNewLine & vbNewLine & _
"test2" & vbNewLine & _
"test3"
On Error Resume Next
With xOutMail
.To = "test@test.com"
.CC = ""
.BCC = ""
.Subject = "send by cell value test"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
MsgBox "Mail verschickt!"
Else
MsgBox "Abgebrochen!"
End If
End Sub
更新(新代码):
我已经稍微更改了我的代码,不幸的是我现在有一个“无限”循环,发送邮件现在被触发了大约 10 次...... 可能有人能够看到为什么会发生这种情况的问题? (现在至少它会为我想要的每个单元格触发)
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Intersect(Range("A4", "H4"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("I4", "L4"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 31 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("A10", "D10"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 31 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("E10", "H10"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("I10"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 51 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("K10"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("A16", "F16"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 31 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("G16", "J16"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("K16"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 3 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("A22", "L22"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("A28", "F28"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("A57"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 26 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("D57"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 16 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("G57"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("A65"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("D65", "H65"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("A79", "E79"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("A94", "H94"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("A100", "H100"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
End If
End If
If Intersect(Range("A106"), Target) Then
If IsNumeric(Target.Value) And Target.Value < 2 Then
Call Mail_small_Text_Outlook
End If
End If
End Sub
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
If MsgBox("Senden?", vbOKCancel) = vbOK Then
xMailBody = "test" & vbNewLine & vbNewLine & _
"test2" & vbNewLine & _
"test3"
On Error Resume Next
With xOutMail
.To = "test@test.com"
.CC = ""
.BCC = ""
.Subject = "send by cell value test"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
MsgBox "Mail verschickt!"
Else
MsgBox "Abgebrochen!"
End If
End Sub
更新2:
太好了,它现在正在使用此代码:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Range("A4", "H4"), Target) Is Nothgin Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("I4", "L4"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 31 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("A10", "D10"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 31 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("E10", "H10"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("I10"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 51 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("K10"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("A16", "F16"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 31 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("G16", "J16"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("K16"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 3 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("A22", "L22"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("A28", "F28"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("A57"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 26 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("D57"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 16 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("G57"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("A65"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("D65", "H65"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 21 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("A79", "E79"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("A94", "H94"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("A100", "H100"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 6 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
If Not Intersect(Range("A106"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value < 2 Then
Call Mail_small_Text_Outlook
Exit Sub
End If
End If
End Sub
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
If MsgBox("Senden?", vbOKCancel) = vbOK Then
xMailBody = "test" & vbNewLine & vbNewLine & _
"test2" & vbNewLine & _
"test3"
On Error Resume Next
With xOutMail
.To = "test@test.com"
.CC = ""
.BCC = ""
.Subject = "send by cell value test"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
MsgBox "Mail verschickt!"
Else
MsgBox "Abgebrochen!"
End If
End Sub
【问题讨论】:
标签: excel vba email conditional-statements