当cell = X时如何停止命令运行

时间:2019-04-11 13:12:25

标签: excel vba outlook

设置宏,以便在第N行的单元格为yes且第J行的单元格为空白时发送电子邮件。如果同时满足这两个要求,则会发送电子邮件,然后将日期插入到J行中(这样可以防止宏再次在该行上运行)。

即使在J行中有一个日期,下面的代码也将运行该宏,但我不知道为什么。

(我是初学者)

Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")

On Error GoTo cleanup
   For Each cell In Columns("H").Cells.SpecialCells(xlCellTypeConstants)
    If cell.Value Like "?*@?*.?*" And _
       LCase(Cells(cell.Row, "N").Value) = "yes" _
       And LCase(Cells(cell.Row, "J").Value) <> Format(Date, "dd/mm/yyyy") Then

        Set OutMail = OutApp.CreateItem(0)

        On Error Resume Next
        With OutMail
            .To = cell.Value
            .Subject = "Review - " & Cells(cell.Row, "C").Value
            .HTMLBody = "<HTML><BODY>Greetings, <br />" & _
            "<br />" & _
            "I hope this email finds you well. <br />" & _
            "<br />" & _
            "We need your review back by " & DateAdd("ww", 3, Date) & ", as we are keen to forward on feedback. <br />" & _
            "<br />" & _
            "Kind regards,</BODY></HTML>"
            .Send
        End With
        On Error GoTo 0
        Cells(cell.Row, "J").Value = Format(Date, "dd/mm/yyyy")
        Set OutMail = Nothing
    End If
   Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

0 个答案:

没有答案