Looping through sheet to end emails from excel

chcoder

New Member
Joined
Nov 30, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi I'm trying to set up emails to send out based on the list of names, email addresses and body text in the excel sheet. each line is for a new email recipient. I created and tested code that works perfectly for sending to just this one email but now I want code that would loop and would send more emails if i had another employee in line 3, 4, ..ect.

.
1639589603691.png


this is the code that worked for me:


Sub sending_the_email_worked()

Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem
Set OutlookApp = New Outlook.Application
Set OutlookMail = OutlookApp.CreateItem(olMailItem)



Dim ToName As String

Dim EmpName As String

Dim strMsg As String


ToName = Range("A2").Value
EmpName = Range("B2").Value
strMsg = Range("C2").Value

With OutlookMail

.To = ToName
.CC = ""
.BCC = ""
.Subject = EmpName & ", Your timesheets needs attention!"

.Display

.HTMLBody = "<p style='font-family:calibri;font-size:16'>Hi " & EmpName & "," & "<br><br>" & strMsg & "<br><br>" & "Thank you,</p>" & .HTMLBody


.Send



End With
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try this:

VBA Code:
Sub sending_the_email_worked()
  Dim OutlookApp As Outlook.Application
  Dim OutlookMail As Outlook.MailItem
  Dim i As Long

  For i = 2 To Range("A" & Rows.Count).End(3).Row
    Set OutlookApp = New Outlook.Application
    Set OutlookMail = OutlookApp.CreateItem(olMailItem)
    With OutlookMail
      .To = Range("A" & i).Value
      .Subject = Range("B" & i).Value & ", Your timesheets needs attention!"
      .Display
      .HTMLBody = "<p style='font-family:calibri;font-size:16'>Hi " & Range("B" & i).Value & "," & _
        "<br><br>" & Range("C" & i).Value & "<br><br>" & "Thank you,</p>" & .HTMLBody
      .Send
    End With
  Next
End Sub
 
Upvote 0
Thank you this worked!
I just want to understand this code - what does the .End(3).Row mean ?

For i = 2 To Range("A" & Rows.Count).End(3).Row
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,462
Members
448,965
Latest member
grijken

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