繁体   English   中英

Excel VBA从日期起每28天发送一次电子邮件提醒

[英]Excel VBA to send email reminder every 28 days from a Date

我已经有下面的代码可以正常工作了,但是我想有一种比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

使用模运算符可实现此目的。 当您不想将来每隔28个位置发送一封邮件时,可能需要注意一些条件。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM