Do Until IsEmpty loop check value condition and then send email

aagarwal

New Member
Joined
May 10, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have and excel sheet which has Environment name in column A and it should send email to the respective group until the cell value is empty.
Also in Column D, I have the expiry dates and status and if the expiry date is equal or less than 60 days, it should send email automatically.

My problem is how to set the until loop or any other alternative to enter every cell and check the condition

Code that I have used atm:

VBA Code:
Private Sub CommandButton3_Click()

Dim OutlookApp As Object, OutLookMailItem As Object
Dim i As Byte, row_num As Byte
row_num = 2

Set OutlookApp = CreateObject("Outlook.application")
Set OutLookMailItem = OutlookApp.CreateItem(0)

i = 2

Do Until IsEmpty(Cells(i, 1))

    With OutLookMailItem
        .To = "email@gmail.com"
        .Subject = "[RESPONSE REQUIRED] Client's Environment Expiry Alert Notification - " & Sheets(2).Cells(row_num, 2)
        .Body = "Hi Team" & vbNewLine & vbNewLine _
                & "The Client Account : " & Sheets(2).Cells(row_num, 3) & "' with the  environment name - '" & Sheets(2).Cells(row_num, 2) _
                & "' is expiring on " & Sheets(2).Cells(row_num, 4)
                
        .send

    End With

row_num = row_num + 1

Loop

Set OutLookMailItem = Nothing
Set OutlookApp = Nothing

End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You are never incrementing i inside your loop, so if cell A2 is not empty, your loop becomes an infinite loop.

What is the relationship between i and row_num? Are they really supposed to be the same row?
 
Upvote 0
You are never incrementing i inside your loop, so if cell A2 is not empty, your loop becomes an infinite loop.

What is the relationship between i and row_num? Are they really supposed to be the same row?
I tried giving i=i+1 but still doesn't work as expected. It just sends out email only for the first row.
There is no relation between
VBA Code:
i
and
VBA Code:
row_num
. Both are separate.
VBA Code:
row_num
is used to reference the values from the sheet while formatting the email body.

1652190563554.png
 
Upvote 0
Client Name
Environment Name
Account Name
Expiry Date
Status
HelloHelloHello
8/05/2022​
Already Expired 2 Day(s) ago
HeyHeyHey
18/05/2022​
Going to Expire in 8 Days
HolaHolaHola
30/06/2022​
Going to Expire in 51 Days
IndiaIndiaIndia
30/06/2022​

Going to Expire in 51 Days
AusAusAus
1/10/2022​
Going to Expire in 144 Days
CanadaCanadaCanada
1/10/2022​
Going to Expire in 144 Days
 
Upvote 0
There is no relation between
VBA Code:
i
and
VBA Code:
row_num
. Both are separate.
But if they both start as 2, and are both incremented in each pass of the loop, then they are the same. You are processing each row until column A is empty, and using that same row to populate the email. They refer to exactly the same thing.

The problem you are having is that you are reusing the same email object every time. You need to create a new one each time. I have moved that inside your loop.

This code makes those two changes and works perfectly for me:

VBA Code:
Private Sub CommandButton3_Click()

   Dim OutlookApp As Object, OutLookMailItem As Object
   Dim row_num As Byte
   row_num = 2
 
   Set OutlookApp = CreateObject("Outlook.application")
 
   Do Until IsEmpty(Cells(row_num, 1))
 
       Set OutLookMailItem = OutlookApp.CreateItem(0)
       With OutLookMailItem
           .To = "email@gmail.com"
           .Subject = "[RESPONSE REQUIRED] Client's Environment Expiry Alert Notification - " & Sheets(2).Cells(row_num, 2)
           .Body = "Hi Team" & vbNewLine & vbNewLine _
                   & "The Client Account : " & Sheets(2).Cells(row_num, 3) & "' with the  environment name - '" & Sheets(2).Cells(row_num, 2) _
                   & "' is expiring on " & Sheets(2).Cells(row_num, 4)
                
           .send
 
       End With
 
       row_num = row_num + 1
 
   Loop
 
   Set OutLookMailItem = Nothing
   Set OutlookApp = Nothing

End Sub
 
Upvote 0
But if they both start as 2, and are both incremented in each pass of the loop, then they are the same. You are processing each row until column A is empty, and using that same row to populate the email. They refer to exactly the same thing.

The problem you are having is that you are reusing the same email object every time. You need to create a new one each time. I have moved that inside your loop.

This code makes those two changes and works perfectly for me:

VBA Code:
Private Sub CommandButton3_Click()

   Dim OutlookApp As Object, OutLookMailItem As Object
   Dim row_num As Byte
   row_num = 2
 
   Set OutlookApp = CreateObject("Outlook.application")
 
   Do Until IsEmpty(Cells(row_num, 1))
 
       Set OutLookMailItem = OutlookApp.CreateItem(0)
       With OutLookMailItem
           .To = "email@gmail.com"
           .Subject = "[RESPONSE REQUIRED] Client's Environment Expiry Alert Notification - " & Sheets(2).Cells(row_num, 2)
           .Body = "Hi Team" & vbNewLine & vbNewLine _
                   & "The Client Account : " & Sheets(2).Cells(row_num, 3) & "' with the  environment name - '" & Sheets(2).Cells(row_num, 2) _
                   & "' is expiring on " & Sheets(2).Cells(row_num, 4)
               
           .send
 
       End With
 
       row_num = row_num + 1
 
   Loop
 
   Set OutLookMailItem = Nothing
   Set OutlookApp = Nothing

End Sub
Thank you so much. It worked.!
Also one last thing how do I automate/use if to send emails only if the expiry is less than 60 days?
 
Upvote 0
@6StringJazzer Can you please advise on the query to be used here to send emails only when the "Expiry Date" columns has value less than 90 days?
 
Upvote 0
Do you mean less than 60 days in the future? I believe it would be this.

You should also have a column to track whether you have already sent an email, unless you want to continually send emails to the same person.

Rich (BB code):
Private Sub CommandButton3_Click()

   Dim OutlookApp As Object, OutLookMailItem As Object
   Dim row_num As Byte
   row_num = 2
 
   Set OutlookApp = CreateObject("Outlook.application")
 
   Do Until IsEmpty(Cells(row_num, 1))
 
       If Cells(row_num, "D") < Date + 60 Then

          Set OutLookMailItem = OutlookApp.CreateItem(0)
          With OutLookMailItem
              .To = "email@gmail.com"
              .Subject = "[RESPONSE REQUIRED] Client's Environment Expiry Alert Notification - " & Sheets(2).Cells(row_num, 2)
              .Body = "Hi Team" & vbNewLine & vbNewLine _
                      & "The Client Account : " & Sheets(2).Cells(row_num, 3) & "' with the  environment name - '" & Sheets(2).Cells(row_num, 2) _
                      & "' is expiring on " & Sheets(2).Cells(row_num, 4)
                
              .send
 
          End With

       End If
 
       row_num = row_num + 1
 
   Loop
 
   Set OutLookMailItem = Nothing
   Set OutlookApp = Nothing

End Sub
 
Upvote 0
Solution
Do you mean less than 60 days in the future? I believe it would be this.

You should also have a column to track whether you have already sent an email, unless you want to continually send emails to the same person.

Rich (BB code):
Private Sub CommandButton3_Click()

   Dim OutlookApp As Object, OutLookMailItem As Object
   Dim row_num As Byte
   row_num = 2
 
   Set OutlookApp = CreateObject("Outlook.application")
 
   Do Until IsEmpty(Cells(row_num, 1))
 
       If Cells(row_num, "D") < Date + 60 Then

          Set OutLookMailItem = OutlookApp.CreateItem(0)
          With OutLookMailItem
              .To = "email@gmail.com"
              .Subject = "[RESPONSE REQUIRED] Client's Environment Expiry Alert Notification - " & Sheets(2).Cells(row_num, 2)
              .Body = "Hi Team" & vbNewLine & vbNewLine _
                      & "The Client Account : " & Sheets(2).Cells(row_num, 3) & "' with the  environment name - '" & Sheets(2).Cells(row_num, 2) _
                      & "' is expiring on " & Sheets(2).Cells(row_num, 4)
               
              .send
 
          End With

       End If
 
       row_num = row_num + 1
 
   Loop
 
   Set OutLookMailItem = Nothing
   Set OutlookApp = Nothing

End Sub
Thank you @6StringJazzer
 
Upvote 0
Hi @6StringJazzer
Last one. If I have to send a collated email with the environments that are getting expired instead of sending one by one, how do I do that?
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,315
Members
449,081
Latest member
tanurai

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