Excel VBA每隔28天发送一次电子邮件提醒

时间:2018-01-18 08:02:41

标签: excel vba excel-vba

我已经有下面的代码可以使用,但我想有一个比28天内有很多if语句更好的方法。

有人可以告诉我怎么说每28天根据创建日期发送电子邮件?

谢谢!

Sub Current28()
'
'
'
'Dim Answer As VbMsgBoxResult
'Answer = MsgBox("Are you sure you want to run?", vbYesNo, "Run Macro")
'If Answer = vbYes Then

Dim i As Integer, Mail_Object, Email_Subject, o As Variant, lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
Dim created As String
'Dim position As String
'Dim branch As String

Set Mail_Object = CreateObject("Outlook.Application")
For i = 2 To lr
    With Mail_Object.CreateItem(o)
        .Subject = "Current Vendor Care 28 Days"
        .To = "mail@myemail.co.uk"
        .Body = Range("D" & i).Value & " " & Range("E" & i).Value & " " & 
Range("F" & i).Value
        '.display
        ' Our data below
        created = Range("L" & i).Value
        'position = Range("N" & i).Value
        'branch = Range("T" & i).Value

' Branch logic

'If branch = "Herne Bay" Then .To = "mail@myemail"
'If branch = "Whitstable" Then .To = "whitstable@myemail"

' Send logic

If DateDiff("d", created, Date - 28) = 0 Then .send
If DateDiff("d", created, Date - 56) = 0 Then .send
If DateDiff("d", created, Date - 84) = 0 Then .send
If DateDiff("d", created, Date - 140) = 0 Then .send
If DateDiff("d", created, Date - 168) = 0 Then .send
If DateDiff("d", created, Date - 196) = 0 Then .send
If DateDiff("d", created, Date - 224) = 0 Then .send

End With
Next i
    'MsgBox "E-mails successfully sent", 64
    'Application.DisplayAlerts = False

Set Mail_Object = Nothing

' The End If below relates to the run yes or no box
'End If

End Sub

1 个答案:

答案 0 :(得分:0)

使用模运算符来实现此目的。如果您不想在将来的某个地方每隔28发送一封邮件,您可能需要处理这些问题。

If DateDiff("d", created, Date) Mod 28 = 0 Then .send