[英]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.