根据多种条件从excel发送自动邮件

时间:2015-07-10 12:48:41

标签: excel vba email multiple-conditions

我是VBA新手,所以提前感谢任何可以帮助我的人。基本上我使用改编的Ron de Bruin代码,当他们的出勤率下降到特定级别的特定级别时自动向学生发送邮件。到目前为止,很好,Ron de Bruin的东西照顾了这一点。 但是还有另一个我要添加的标准,基本上只有在发送邮件时才发送邮件' Y'在与出勤相同的行中的不同单元格中。 总而言之,我只希望邮件发送给符合这两个标准的人,1)降到一定水平以下,2)有一个' Y'在另一个单元格中,但此刻的代码仅考虑了第一个标准。非常感谢。 Alun(下面的代码)

Option Explicit

Private Sub Worksheet_Calculate()


Dim FormulaRange As Range
Dim NotSentMsg As String
Dim MyMsg As String
Dim SentMsg As String
Dim MyLimit As Double



NotSentMsg = "Not Sent"
SentMsg = "Sent"

'Above the MyLimit value it will run the macro
MyLimit = 80

'Set the range with Formulas that you want to check
Set FormulaRange = Me.Range("BH279:BH280")

On Error GoTo EndMacro:
For Each FormulaCell In FormulaRange.Cells
    With FormulaCell
        If IsNumeric(.Value) = False Then
            MyMsg = "Not numeric"
        Else
            If .Value < MyLimit Then


                MyMsg = SentMsg
                If .Offset(0, 1).Value = NotSentMsg Then
                    Call Mail_with_outlook2
                End If
            Else
                MyMsg = NotSentMsg
            End If
        End If
        Application.EnableEvents = False
        .Offset(0, 1).Value = MyMsg
        Application.EnableEvents = True
    End With
Next FormulaCell

ExitMacro:
Exit Sub

EndMacro:
Application.EnableEvents = True

MsgBox "Some Error occurred." _
     & vbLf & Err.Number _
         & vbLf & Err.Description

End Sub

1 个答案:

答案 0 :(得分:0)

If .Value2 < MyLimit And Not .EntireRow.Find(What:="Y", LookIn:=xlValues, LookAt:=xlWhole) Is Nothing Then Call Mail_with_outlook2
'If you look for the complementer solution, remove the " Not"

您在同一行中查找值"Y" 我建议将输出变量设置为邮件宏Call Mail_with_outlook2(emailaddress, name, title, MyValue)