VBA for Sending to a list of Emails and continue to bottom of last Row

craigw02

New Member
Joined
Sep 12, 2013
Messages
26
Hi there I have the attached sheet which I have started VBA coding to send emails to a list of email addresses, but I want it to continue to the bottom of the very last row of the excel spreadsheet and then stop. So far this only does the very first email address on row A1 but I also wish for it to send the email if column "G2" says "YES" down to the last row also if this makes sense?


Sub CreateMail()

Dim objOutlook As Object
Dim objMail As Object
Dim rngTo As Range
Dim rngSubject As Range
Dim rngBody As Range
Dim rngAttach As Range

Set objOutlook = CreateObject("Outlook.Application")
Set objMail = objOutlook.CreateItem(0)

With ActiveSheet
Set rngTo = .Range("a2")
Set rngSubject = .Range("e2")
Set rngCC = .Range("c2")
Set rngBody = .Range("f2")
Set rngAttach = .Range("d2")
End With

With objMail
.To = rngTo.Value
.Subject = rngSubject.Value
.Body = rngBody.Value
.Attachments.Add rngAttach.Value
.Display 'Instead of .Display, you can use .Send to send the email _
or .Save to save a copy in the drafts folder
.CC = rngCC.Value
End With

Set objOutlook = Nothing
Set objMail = Nothing
Set rngTo = Nothing
Set rngSubject = Nothing
Set rngBody = Nothing
Set rngAttach = Nothing

End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try this...


Code:
[color=darkblue]Sub[/color] CreateMail()
    
    [color=darkblue]Dim[/color] objOutlook [color=darkblue]As[/color] [color=darkblue]Object[/color]
    [color=darkblue]Dim[/color] cell       [color=darkblue]As[/color] Range
    
    [color=darkblue]Set[/color] objOutlook = CreateObject("Outlook.Application")
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] cell [color=darkblue]In[/color] Range("A2", Range("A" & Rows.Count).End(xlUp))
        [color=darkblue]If[/color] UCase(cell.Range("G1").Value) = "YES" [color=darkblue]Then[/color]
            [color=darkblue]With[/color] objOutlook.CreateItem(0)
                .to = cell.Value
                .CC = cell.Range("C1").Value
                .Attachments.Add cell.Range("D1").Value
                .Subject = cell.Range("E1").Value
                .Body = cell.Range("F1").Value
                .Display    [color=green]'Instead of .Display, you can use .Send to send the email _
                             or .Save to save a copy in the drafts folder[/color]
            [color=darkblue]End[/color] [color=darkblue]With[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] cell
    
    [color=darkblue]Set[/color] objOutlook = [color=darkblue]Nothing[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,222,227
Messages
6,164,717
Members
451,912
Latest member
HMF009

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