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
 
I don't think "collated" is the right word here. You want to send a single email that lists all expired environments, instead of sending an email for each one? I have no way to test this but this should work:

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")
 
   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
                
      Do Until IsEmpty(Cells(row_num, 1))
    
          If Cells(row_num, "D") < Date + 60 Then
             
             .body = .body & 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)
   
          End If
    
          row_num = row_num + 1
    
      Loop
    
     .send
          
   End With
   
   Set OutLookMailItem = Nothing
   Set OutlookApp = Nothing

End Sub
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,214,987
Messages
6,122,618
Members
449,092
Latest member
amyap

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