Help needed with Excel VBA code for sending E-mails only to people based on project status change condition

racsng

New Member
Joined
Jun 2, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,
I need to write a code where out of a dynamic list of records in Excel, I need to send mails only to people (say to 3 out of 5 records) whose project status change date is approaching within 7 days. These people need to be notified about due date and are required to change project status in the system.

Condition:
Only if when due date is within 7 days, mail will be sent (display first for any edits) from outlook to the project owner, cc two other persons

Issue:
My code is returning only one e-mail, that to for the last record in the given list. I want it to display mails for all cases that satisfy given condition.

Table headers are in row 3 and data rows start from row 4.

I would really appreciate any help with the code below. Thank you!


Sample data:
ABCDEFG
Project NameNo. of days DuePM First NameAM e-mailEmail Cc1Email Cc2E-mail Message
ABC7PM1PM1@abc.comCC1@abc.comCC2@abc.comChange status
CDE12PM2PM2@abc.comCC1@abc.comCC2@abc.com
PQR6PM3PM3@abc.comCC1@abc.comCC2@abc.comChange status
LMN15PM4PM4@abc.comCC1@abc.comCC2@abc.com
TGS7PM5PM5@abc.comCC1@abc.comCC2@abc.comChange status


VBA Code:

VBA Code:
Option Explicit

Sub OptInFlagNotification()

Application.ScreenUpdating = False

'Defining outlook variables
Dim OutApp As Object
Dim OutMail As Object

'Variables allocated
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

Dim EmailTo As String
Dim EmailCc1 As String
Dim EmailCc2 As String
Dim EmailToAddress As String
Dim EmailMessage As String
Dim LastRow As Integer
Dim RowCounter As Integer


Dim NextParagraph As String
NextParagraph = vbNewLine & vbNewLine


LastRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

For RowCounter = 4 To LastRow
   
' to run only when E-mail message is not blank in column G , i.e. when due is date within 7 days from now
    If Cells(RowCounter, 6).Value <> "" Then
   
        EmailToAddress = Sheet1.Range("D" & RowCounter).Value
        EmailCc1 = Sheet1.Range("E" & RowCounter).Value
        EmailCc2 = Sheet1.Range("F" & RowCounter).Value
        EmailTo = Sheet1.Range("C" & RowCounter).Value
        EmailMessage = Sheet1.Range("G" & RowCounter).Value
       
       
       With OutMail
                .to = EmailToAddress
                .cc = EmailCc1 & "; " & EmailCc2
                .Subject = "Project status change Alert"
                .Body = "Hi " & EmailTo & NextParagraph _
                & EmailMessage _
                & NextParagraph & "Thank you," & vbNewLine & _
                "XYZ"
                .Display
              
         End With
               
     End If
    

Next RowCounter

Set OutApp = Nothing
Set OutMail = Nothing

Application.ScreenUpdating = True


End Sub
 
Last edited by a moderator:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I'll bet you didn't step through this code and watch what happens. That's trouble shooting 101. I can tell you what's wrong and how to fix it, but would you rather try practicing that trouble shooting method for yourself first?
Clue - add RowCounter to the email body for proof positive.
 
Upvote 0
Solution
I'll bet you didn't step through this code and watch what happens. That's trouble shooting 101. I can tell you what's wrong and how to fix it, but would you rather try practicing that trouble shooting method for yourself first?
Clue - add RowCounter to the email body for proof positive.
Thanks for the reply & clue. I'm kind of a beginner!
I did step through code & also tried counter to the mail body - not working for me, still getting to see only one final e-mail message to edit in the end! There should be 3 windows for different e-mails displayed as the final outcome.
 
Upvote 0
I'll bet you didn't step through this code and watch what happens. That's trouble shooting 101. I can tell you what's wrong and how to fix it, but would you rather try practicing that trouble shooting method for yourself first?
Clue - add RowCounter to the email body for proof positive.
Finally got it! Thank you!

I've done following change to the code:
added counter 'i'

VBA Code:
Dim i as integer

i = 4
For RowCounter = 4 To LastRow
   
' to run only when E-mail message is not blank in column G , i.e. when due is date within 7 days from now
    If Cells(RowCounter, 6).Value <> "" Then
   
        EmailToAddress = Sheet1.Range("D" & RowCounter).Value
        EmailCc1 = Sheet1.Range("E" & RowCounter).Value
        EmailCc2 = Sheet1.Range("F" & RowCounter).Value
        EmailTo = Sheet1.Range("C" & RowCounter).Value
        EmailMessage = Sheet1.Range("G" & RowCounter).Value
       
       
       With OutMail
                .to = EmailToAddress
                .cc = EmailCc1 & "; " & EmailCc2
                .Subject = "Project status change Alert"
                .Body = "Hi " & EmailTo & NextParagraph _
                & EmailMessage _
                & NextParagraph & "Thank you," & vbNewLine & _
                "XYZ"
                .Display
              
         End With

i = i + 1
               
     End If
    

Next RowCounter
 
Upvote 0
I was thinking
For RowCounter = 4 To LastRow
Set OutMail = OutApp.CreateItem(0)

so that the object is created on each pass, but if your way works for you, then that's good too. Your problem was that it was being replaced. That's why I suggested adding the variable; you'd see that the only email you ended up with was the last one. Not sure why you start at row 4 based on your sample data? You might want to set up a limit on a loop like this. A little mistake can have you creating thousands of emails by accident.
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top