I am trying to update an excel sheet to send reminders to certain people. This excel sheet has a list of people and when they were certified to use certain equipment. Previously, when their certification was about to expire, or had expired, they would have to be told manually. I have the sheet set up so that it automatically calculates how many days it has been since they received certification, as well as how many days before they have to recertify. What I am trying to do is make a script that reads the column with how many days the person has before recertification, and if it is less than, say, 30 days, then the script will automatically send an email.
I have followed a tutorial on how to build a script that sends emails, but it sends them to all that are listed. I tried to modify it to check the "F" column, where that "days since certification," and only send it to those that are less than or equal to 30, but I am admittedly out of my depth in even basic coding. Here is my attempt that did not work. I have bolded the parts that I put in, everything else is code I followed a tutorial for and works perfectly. When I try and run this, it says "Compile Error Next Without For." Not really sure how I would do this.
Sub CreateCourseCertificates()
Dim EApp As Object
Set EApp = CreateObject("Outlook.Application")
Dim EItem As Object
Dim RList As Range
Set RList = Range("A2", Range("a2").End(xlDown))
Dim R As Range
Dim daysSinceCert As Range
Set daysSinceCert = Range("F2", Range("f2").End(xlDown))
For Each R In RList
If daysSinceCert <= 30 Then
Set EItem = EApp.CreateItem(0)
With EItem
.To = R.Offset(0, 2)
.Subject = "Test"
.Body = "Test"
.Display
End With
Next R
Set EApp = Nothing
Set EItem = Nothing
End Sub
Thank you.
>Solution :
Remove
Dim daysSinceCert As Range
Set daysSinceCert = Range("F2", Range("f2").End(xlDown))
and instead use Offset to refer to the corresponding cell in column F as you loop:
For Each R In RList
If R.Offset(,5).Value <= 30 Then
Set EItem = EApp.CreateItem(0)
With EItem
.To = R.Offset(,2).Value
.Subject = "Test"
.Body = "Test"
.Display
End With
End If
Next