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

sending an auto mail from excel based on multiple conditions

我是 VBA 新手,所以在此先感谢任何可以帮助我的人。基本上,当学生的出勤率低于特定 excel 单元格中显示的特定水平时,我使用经过改编的 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
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).