Hello experts,
I am quite new to VBA. Currently, I am working on a project where I want to send an automatic email seven days before the due date notifying that the due date is approaching. I want to get the date and the respective email address from two different sheets. I searched for other threads to find a code, but couldn't find an exact match. So, I modified a code I found in the internet a little, but I get an error message. It will be a great help if anyone of you could help me to find a way to correct that error.
Here is the code that I am using :
This is my workbook
This is the error message I get once I run the above code:
Thanks a lot in advance
I am quite new to VBA. Currently, I am working on a project where I want to send an automatic email seven days before the due date notifying that the due date is approaching. I want to get the date and the respective email address from two different sheets. I searched for other threads to find a code, but couldn't find an exact match. So, I modified a code I found in the internet a little, but I get an error message. It will be a great help if anyone of you could help me to find a way to correct that error.
Here is the code that I am using :
VBA Code:
Option Explicit
Sub email()
Dim r As Range
Dim cell As Range
Set r = Range("U2:U10000")
For Each cell In r
If cell.Value = Date + 7 Then
Dim Email_Subject, Email_Send_From, Email_Send_To, _
Email_Cc, Email_Body As String
Dim Mail_Object, Mail_Single As Variant
Dim Machine_Code As Long
Dim Machine_Type As Long
Machine_Code = Application.WorksheetFunction.VLookup(cell.Value, Range("A:U"), 21, False)
Machine_Type = Application.WorksheetFunction.VLookup(Machine_Code, Sheet1.Range("B:C"), 1, False)
Email_Subject = "Service Reminder"
Email_Send_From = "k******@*******"
Email_Send_To = Application.WorksheetFunction.VLookup(Machine_Code, Sheet1.Range("C:M"), 11, False)
'Email_Cc = "D@******.com"
Email_Body = "There is a Service scheduled for a" & Machine_Type & "on" & cell.Value
On Error GoTo debugs
Set Mail_Object = CreateObject("Outlook.Application")
Set Mail_Single = Mail_Object.CreateItem(0)
With Mail_Single
.Subject = Email_Subject
.To = Email_Send_To
.cc = Email_Cc
.Body = Email_Body
.send
End With
End If
Next
Exit Sub
debugs:
If Err.Description <> "" Then MsgBox Err.Description
End Sub
This is my workbook
This is the error message I get once I run the above code:
Thanks a lot in advance