简体   繁体   中英

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

I have the below code already that works but I guess there is a much better way than having lots of if statements going up in 28 days.

Could someone please show me how to say Every 28 days send email based on date created?

Thank You!


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

Use modulo operator to achieve this. You might need to take care of conditions when you not want to send an mail every 28 somewhere in the future.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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