Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How would I use the VBA to send an email only to people who are assigned a certain value?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading