Need Help with Excel Macro
I have a large excel sheet (macro enabled) that I want to send email from with specific data in each email. I have all working right but I can't get it to create an email to send to each email address with that email address' specific SUBJECT. My macro runs but puts the same email address in the TO and the same subject in the SUBJECT in each email. FYI - my macro also puts in a date that the email was sent. You'll need to delete this date each time you run the macro or it won't run.
I'm about ready to give up...will someone please help me?? Here is the code I'm using. I've also attached the spreadsheet.
Sub Macro1()
Dim mApp As Object
Dim rngCell As Range
Dim rngMyDataSet As Range
Dim Rng As Range
Dim OutApp As Object
Dim mMail As Object
Dim OutMail As Object
Dim EmailSubject As String
Dim SendToMail As String
Dim EmailRecipient As Range
Dim Signature As String
With ActiveSheet
If .FilterMode Then .ShowAllData
Set Rng = .Range("AH5", .Cells(.Rows.Count, 1).End(xlUp))
End With
For Each rngCell In Rng
If rngCell.Offset(0, 6) > 0 Then
ElseIf rngCell.Offset(0, 5) > Evaluate("Today() +7") And _
rngCell.Offset(0, 5).Value <= Evaluate("Today() +120") Then
rngCell.Offset(0, 6).Value = Date
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "According to my records, your " & Range("A5").Value & " contract is due for review " & rngCell.Offset(0, 5).Value & _
". It is important you review this contract ASAP and email me with any changes made. If it is renewed, please fill out the Contract Cover Sheet which can be found in the Everyone folder and send me the cover sheet along with the new original contract."
SendToMail = Sheets("sheet1").Range("AH5").Value
EmailSubject = Sheets("sheet1").Range("A5").Value
Signature = "C:\Documents and Settings\" & Environ("rmm") & _
"\Application Data\Microsoft\Signatures\rm.htm"
On Error Resume Next
For Each r In Selection
SendToMail = Range("AH" & r.Row)
MailSubject = Range("A" & r.Row)
mMailBody = Range("AO" & r.Row)
Set mApp = CreateObject("Outlook.Application")
Set mMail = mApp.CreateItem(0)
With mMail
.To = SendToMail
.Subject = MailSubject
.Body = mMailBody
.Display ' You can use .Send
End With
Next r
Application.ScreenUpdating = False
End If
Next rngCell
Application.ScreenUpdating = True
End Sub
See the attached version. The code is in Module1.