+ Reply to Thread
Results 1 to 23 of 23

EXCEL VBA send dynamic emails messages based on trigger column

Hybrid View

  1. #1
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    EXCEL VBA send dynamic emails messages based on trigger column

    I am attempting to send emails to a distribution list based on a trigger column (column E). Each row that contains the word "Captured" should send an email to the address in column A. The email body for these outgoing emails should contain content in column D. Ideally I would like the Name (B) and Client (C) to dynamically appear in the Subject of the email.


    The code below, fires to the correct recipients but sends the same message body (last one within the list to all email addresses).

    I am in need of this for a large project so any help would be amazing!
    Dim outlookapp As Object
    Dim Outlookmailitem As Object
    Dim Icounter As Integer
    Dim maildest As String
    Dim mess As String
    
    Set outlookapp = CreateObject("outlook.application")
    Set Outlookmailitem = outlookapp.CreateItem(0)
    
    With Outlookmailitem
    maildest = ""
    For Icounter = 1 To WorksheetFunction.CountA(Columns(1))
    If maildest = "" And Cells(Icounter, 1).Offset(0, 4) = "Captured" Then
    mess = Cells(Icounter, 4).Value
    maildest = Cells(Icounter, 1).Value
    
    ElseIf maildest <> "" And Cells(Icounter, 1).Offset(0, 4) = "Captured" Then
    mess = Cells(Icounter, 4).Value
    maildest = maildest & ";" & Cells(Icounter, 1).Value
    
    End If
    Next Icounter
    
    .CC = maildest
    .Subject = "name and Client"
    .Body = mess
    .Send
    End With
    
    Set Outlookmailitem = Nothing
    Set outlookapp = Nothing
    End Sub
    Last edited by mcjonesy; 04-17-2016 at 09:41 PM.

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    post withdrawn
    Last edited by humdingaling; 04-17-2016 at 09:53 PM. Reason: OP complied
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    I have posted my code per your suggestion. Sorry for the mistake.

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    reason why your receipts are dynamic but nothing else is because of this line

    maildest = maildest & ";" & Cells(Icounter, 1).Value
    you dont have something similar for your mess or your subject
    to get subject in there you should have a variable to capture subject as well

    how exactly do you want to structure your subject and your "mess"

    ps why are you using
    Cells(Icounter, 1).Offset(0, 4)
    instead of just
    Cells(Icounter, 5)
    ?

  5. #5
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    I used
    Cells(Icounter, 1).Offset(0, 4)
    because those were the instructions of the tutorial I watched. https://www.youtube.com/watch?v=Em4RCvsAV7s

    I am open to whatever is easiest. This video did not show me how to dynamically set subject and dynamic messages. I would like to send a message when Captured is identified to look like the following.

    Subject "Monsanto (client C) Application Development (b name) Opportunity"

    Message body: "This would be a sentence extracted by whatever was typed into Column D of the related row"







    Email First Last opportunity email
    [email protected] Mike Peters I will be working at express scripts.
    [email protected] John smith I am a excel guru TEST
    [email protected] robyn williams ABI here I come. YUMMY! TEST

  6. #6
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    this is very close. I tried out your attachment, the only thing is that each email should be sent separately not in one joint email. So in your example there should be 4 separate emails. Any suggestions?

  7. #7
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    i've reconstructed what i think your spreadsheet looks like and added some sample code of what you could possibly do
    Sub EmailTest()
    
        Dim outlookapp As Object 'mailapp object
        Dim Outlookmailitem As Object 'mailitem object
        Dim Icounter As Integer 'counter for loop
        Dim MailDest As String 'email destination
        Dim Mess As String 'message
        Dim Subject As String 'subject
        
        Set outlookapp = CreateObject("outlook.application")
        Set Outlookmailitem = outlookapp.CreateItem(0)
        
        With Outlookmailitem
            
            MailDest = ""
            For Icounter = 2 To WorksheetFunction.CountA(Columns(1))
                If MailDest = "" And Cells(Icounter, 5) = "Captured" Then
                    Mess = Cells(Icounter, 4).Value
                    MailDest = Cells(Icounter, 1).Value
                    Subject = Cells(Icounter, 2).Value & " - " & Cells(Icounter, 3).Value
                
                ElseIf MailDest <> "" And Cells(Icounter, 5) = "Captured" Then
                    Mess = Mess & vbNewLine & Cells(Icounter, 4).Value
                    MailDest = MailDest & ";" & Cells(Icounter, 1).Value
                    Subject = Subject & " | " & Cells(Icounter, 2).Value & " - " & Cells(Icounter, 3).Value
                
                End If
            Next Icounter
            
            .To = MailDest 'not sure why you were using .cc instead of .to
            .Subject = Subject
            .Body = "List of Messages" & vbNewLine & vbNewLine & Mess & vbNewLine & vbNewLine & "Yours Sincerly," & _
                vbNewLine & "McJonesy" 'change to however you want email to be displayed, use vbnewline to enter new line
            .display '.Send change to .send for automatic send, use .display when debugging
            
        End With
        
        Set Outlookmailitem = Nothing
        Set outlookapp = Nothing
        
    End Sub
    see if you does what you want
    ps i changed .send to .display so it doesnt automatically send out emails while you are debugging
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    I don't want to make things to complicated but can separate emails can be sent only for unique email addresses? In your example if row three and nine were both [email protected] with two different messages. Is it possible to group those messages in one email (with a blank line in between) while keeping other rows with different email addresses separate?

  9. #9
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    Quote Originally Posted by mcjonesy View Post
    this is very close. I tried out your attachment, the only thing is that each email should be sent separately not in one joint email. So in your example there should be 4 separate emails. Any suggestions?
    yes, essentially
    you put the mailitems (.to,.subject,.body,etc) within the icounter For/Next Loop
    so it generates seperate email per loop

    Quote Originally Posted by mcjonesy View Post
    I don't want to make things to complicated but can separate emails can be sent only for unique email addresses? In your example if row three and nine were both [email protected] with two different messages. Is it possible to group those messages in one email (with a blank line in between) while keeping other rows with different email addresses separate?
    this is slightly more complicated
    i will need to have a little think on how to do this the easiest way

    how many rows of data are you expecting to run this for?
    50, 1000, 1000000?

  10. #10
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    no more than 100 per email address at once. I will be using code to change the trigger column cell after sending the emails.

    Sub ReplaceTitleMs()
        Dim ws As Worksheet
        
        Set ws = Sheets("eList")
        
        
        ws.Columns("e").Replace What:="Captured", _
                                Replacement:="Sent", _
                                LookAt:=xlPart, _
                                SearchOrder:=xlByRows, _
                                MatchCase:=False, _
                                SearchFormat:=False, _
                                ReplaceFormat:=False
    End Sub

  11. #11
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    no more than 100 per email address at once. I will be using code to change the trigger column cell after sending the emails.

    Sub ReplaceTitleMs()
        Dim ws As Worksheet
        
        Set ws = Sheets("eList")
        
        
        ws.Columns("e").Replace What:="Captured", _
                                Replacement:="Sent", _
                                LookAt:=xlPart, _
                                SearchOrder:=xlByRows, _
                                MatchCase:=False, _
                                SearchFormat:=False, _
                                ReplaceFormat:=False
    End Sub

  12. #12
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    you can do capture > sent code at the same time of the email
    no need to run it seperately

    On unique emails
    can i put a helper column in the data to count number of emails to each email address?
    it would save me putting it in the code itself

    also re:subject
    i changed it to Subject = "Monsanto " & Cells(Icounter, 3).Value & " Application Development " & Cells(Icounter, 2).Value & " Opportunity"
    however in the case there are more than one emails, how do you want subject handled?

  13. #13
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    Yes a helper column would be fine. I can always hide the column when the spreadsheet is being reviewed.

    If more than one message is being sent in an email I suggest the Subject line be hard coded to read "capability Opportunity" and the message body have a table like structure

    Column one Client/ column two would be opportunity name/ column three would be the message specific

    Each row would repeat the format

    I modified your code of where the mailitems (.to,.subject,.body,etc) within the icounter For/Next Loop

    Is this correct?
    Sub EmailTest()
    
        Dim outlookapp As Object 'mailapp object
        Dim Outlookmailitem As Object 'mailitem object
        Dim Icounter As Integer 'counter for loop
        Dim MailDest As String 'email destination
        Dim Mess As String 'message
        Dim Subject As String 'subject
        
        Set outlookapp = CreateObject("outlook.application")
        Set Outlookmailitem = outlookapp.CreateItem(0)
        
        With Outlookmailitem
            
            MailDest = ""
            For Icounter = 2 To WorksheetFunction.CountA(Columns(1))
                If MailDest = "" And Cells(Icounter, 5) = "Captured" Then
                    Mess = Cells(Icounter, 4).Value
                    MailDest = Cells(Icounter, 1).Value
                    Subject = Cells(Icounter, 2).Value & " - " & Cells(Icounter, 3).Value
                
                ElseIf MailDest <> "" And Cells(Icounter, 5) = "Captured" Then
                    Mess = Mess & vbNewLine & Cells(Icounter, 4).Value
                    MailDest = MailDest & ";" & Cells(Icounter, 1).Value
                    Subject = Subject & " | " & Cells(Icounter, 2).Value & " - " & Cells(Icounter, 3).Value
                
                End If
                
            .To = MailDest 'not sure why you were using .cc instead of .to
            .Subject = Subject
            .Body = "List of Messages" & vbNewLine & vbNewLine & Mess & vbNewLine & vbNewLine & "Yours Sincerly," & _
                vbNewLine & "McJonesy" 'change to however you want email to be displayed, use vbnewline to enter new line
                .Send '.Send change to .send for automatic send, use .display when debugging
            
            Next Icounter
            
            '.To = MailDest 'not sure why you were using .cc instead of .to
           ' .Subject = Subject
           ' .Body = "List of Messages" & vbNewLine & vbNewLine & Mess & vbNewLine & vbNewLine & "Yours Sincerly," & _
                vbNewLine & "McJonesy" 'change to however you want email to be displayed, use vbnewline to enter new line
           ' .send '.Send change to .send for automatic send, use .display when debugging
            
        End With
        
        Set Outlookmailitem = Nothing
        Set outlookapp = Nothing
        
    End Sub

  14. #14
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    Sorry to be a pain but I cannot figure out where within the code the .to, subject, body, send need to go in order to send separate emails. If you can re-paste the code with it in its correct place that would be amazing! humdingaling you have been super helpful!

  15. #15
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    it was a bit more complicated then i thought but no need for helper column since you change "Captured" to "sent"
    Sub EmailTest()
    
        Dim outlookApp As Object 'mailapp object
        Dim OutlookMailItem As Object 'mailitem object
        Dim Icounter As Integer 'counter for loop
        Dim MailDest As String 'email destination
        Dim Mess As String 'message
        Dim Subject As String 'subject
        Dim j As Long 'counter for times email appears
        Dim f As Range ' Find and FindNext Range
        Dim RangeToSearch As Range 'define the range of what to search
        Dim fFirst As Range 'First range
        
        For Icounter = 2 To WorksheetFunction.CountA(Columns(1))
                                    
            'reset after each email
            Set outlookApp = CreateObject("outlook.application")
            Set OutlookMailItem = outlookApp.CreateItem(0)
            MailDest = ""
            Subject = ""
            Mess = ""
            j = 0
                
            If Cells(Icounter, 5) = "Captured" Then
                
                With OutlookMailItem
                    
                    'determine how many times emails shows up
                    j = Application.CountIf(Range("A2:A" & WorksheetFunction.CountA(Columns(1))), Cells(Icounter, 1))
                    
                    If j = 1 Then
                        
                        Mess = Cells(Icounter, 4).Value
                        MailDest = Cells(Icounter, 1).Value
                        Subject = "Monsanto " & Cells(Icounter, 3).Value & " Application Development " & Cells(Icounter, 2).Value & " Opportunity"
                        Cells(Icounter, 5) = "Sent"
                    Else
                        
                        Set RangeToSearch = Range("A2:A" & WorksheetFunction.CountA(Columns(1))) 'define range to search
                        Set f = RangeToSearch.Find(Cells(Icounter, 1).Value, lookat:=xlPart, LookIn:=xlValues) 'set first find
                        Set fFirst = f 'set first address
                        
                        'maildest and subject stay the same
                        MailDest = Cells(f.Row, 1).Value
                        Subject = "Monsanto " & Cells(f.Row, 3).Value & " Application Development " & Cells(Icounter, 2).Value & " Opportunity"
                        
                        'define first row of message and update "To send" column
                        Mess = Cells(f.Row, 4).Value
                        Cells(Icounter, 5) = "Sent"
                        
                        Do Until f Is Nothing
                            Set f = RangeToSearch.FindNext(f) 'set next find
                            If f.Address = fFirst.Address Then Exit Do 'exit loop criteria
                            Mess = Mess & vbNewLine & Cells(f.Row, 4).Value 'update mess
                            Cells(f.Row, 5) = "Sent" 'update To Send
                        Loop
                    End If
                    
                    .To = MailDest 'not sure why you were using .cc instead of .to
                    .Subject = Subject
                    .Body = "List of Messages" & vbNewLine & vbNewLine & Mess & vbNewLine & vbNewLine & "Yours Sincerly," & _
                        vbNewLine & "McJonesy" 'change to however you want email to be displayed, use vbnewline to enter new line
                    .display '.Send change to .send for automatic send, use .display when debugging
                End With
                
                Set OutlookMailItem = Nothing
                Set outlookApp = Nothing
                
            End If
            
        Next Icounter
        
    End Sub
    few more variables involved mainly with capturing multiple lines into one email
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    Two bugs:

    1. the code provided sends an email for each occurrence of an email address regardless the rows conditional trigger if one row is listed as Captured

    2. The body of an email with multiple entries lists all rows messages. Only rows with Captured should be listed in the body of the email.


    Enhancement request:

    Of the grouped mass emails their should be one email for each Client.


    Example:

    Pearl has 4 rows with her email address
    Two of the rows are with Client "ABI" with a to send column as "Captured"
    One row is listed as "Express Scripts" with a to send column as "Captured"
    One Row is listed as "RGA" with a to send column as "Sent"

    Pearl will receive two emails.

    1st email

    Subject: ABI (client name) opportunities

    Body:
    • Opportunity 1
    • Opportunity 2

    2nd Email

    Subject: Express Scripts (client name) Opportunities
    Body:
    • Opportunity 1

    Thank you again for your help. You have been a life saver!

  17. #17
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    I took your example and modified it slightly. Bug one from above is resolved. Bug two still exists.

    If the enhancement from my post above isn't possible I would like to format the email body with two columns Cap and Mess. A grid would be ideal.

    Thoughts? Also I would be happy to attach my excel project to these posts but it appears I am not able too.

       Sub victory()
        
        Dim outlookApp As Object 'mailapp object
        Dim OutlookMailItem As Object 'mailitem object
        Dim Icounter As Integer 'counter for loop
        Dim MailDest As String 'email destination
        Dim Mess As String 'message
        Dim cap As String 'capability
        Dim Subject As String 'subject
        Dim j As Long 'counter for times email appears
        Dim f As Range ' Find and FindNext Range
        Dim RangeToSearch As Range 'define the range of what to search
        Dim fFirst As Range 'First range
        
        For Icounter = 2 To WorksheetFunction.CountA(Columns(7))
                                    
            'reset after each email
            Set outlookApp = CreateObject("outlook.application")
            Set OutlookMailItem = outlookApp.CreateItem(0)
            MailDest = ""
            Subject = ""
            Mess = ""
            cap = ""
            j = 0
                
            If Cells(Icounter, 9) = "Captured" Then
                
                With OutlookMailItem
                    
                    'determine how many times emails shows up
                    j = Application.CountIf(Range("G2:G" & WorksheetFunction.CountA(Columns(7))), Cells(Icounter, 7))
                    
                    If j = 1 Then
                        
                        Mess = Cells(Icounter, 8).Value
                        MailDest = Cells(Icounter, 7).Value
                        Subject = Cells(Icounter, 2).Value & "  " & Cells(Icounter, 4).Value & " Opportunities"
                        Cells(Icounter, 9) = "Sent to CSA"
                    Else
                        
                        Set RangeToSearch = Range("G2:G" & WorksheetFunction.CountA(Columns(7))) 'define range to search
                        Set f = RangeToSearch.Find(Cells(Icounter, 7).Value, lookat:=xlPart, LookIn:=xlValues) 'set first find
                        Set fFirst = f 'set first address
                        
                        'maildest and subject stay the same
                        MailDest = Cells(f.Row, 7).Value
                        Subject = "New Capability Opportunities"
                        
                        'define first row of message and update "To send" column
                        Mess = Cells(f.Row, 8).Value
                        cap = Cells(f.Row, 4).Value
                        Cells(Icounter, 9) = "Sent to CSA"
                        
                        Do Until f Is Nothing
                            Set f = RangeToSearch.FindNext(f) 'set next find
                            If f.Address = fFirst.Address Then Exit Do 'exit loop criteria
                            Mess = Mess & vbNewLine & Cells(f.Row, 8).Value 'update mess
                            cap = cap & vbNewLine & Cells(f.Row, 4).Value 'update cap
                            Cells(f.Row, 9) = "Sent to CSA" 'update To Send
                        Loop
                    End If
                    
                    .To = MailDest 'not sure why you were using .cc instead of .to
                    .Subject = Subject
                    .Body = "List of Opportunities:" & vbNewLine & vbNewLine & cap & Mess & vbNewLine & vbNewLine & "Refer to spreadsheet for complete list of all opportunities!" 'change to however you want email to be displayed, use vbnewline to enter new line
                    .Display '.Send change to .send for automatic send, use .display when debugging
                End With
                
                Set OutlookMailItem = Nothing
                Set outlookApp = Nothing
                
            End If
            
        Next Icounter
        
    End Sub
    Last edited by mcjonesy; 04-18-2016 at 05:12 PM.

  18. #18
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    Sorry wasnt able to look at this today
    got bogged down at work

    try these methods for uploading file
    http://www.excelforum.com/the-water-...his-forum.html

  19. #19
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    Posting Project.

    Reasons it didn't allow me to attach:

    1. Document was open

    2. I was using microsoft Edge browser
    Attached Files Attached Files

  20. #20
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    ok i found the issues in your code

    you didnt update the column references properly on every line

    ie
    j = Application.CountIf(Range("D2:D" & WorksheetFunction.CountA(Columns(4))), Cells(Icounter, 4))
    should be
    j = Application.CountIf(Range("G2:G" & WorksheetFunction.CountA(Columns(7))), Cells(Icounter, 7))
    and your rangetosearch wasnt pointing to the right column
    Set RangeToSearch = Range("G2:G" & WorksheetFunction.CountA(Columns(7))) 'define range to search
    in turn it wasnt updating column 9 properly
    Last edited by humdingaling; 04-20-2016 at 08:54 PM. Reason: Use file attached in post #21

  21. #21
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    so i had a chance to properly review your code
    it seems you had two running variables "cap" and "mess" to change essentially one line of Body
    this seems unnecessary and can lead to confusion so i combined into one

    also got rid of mutliple CountA lines and replaced them with one variable that you calculate once

    Sub victory()
        
        Dim outlookApp As Object 'mailapp object
        Dim OutlookMailItem As Object 'mailitem object
        Dim Icounter As Integer 'counter for loop
        Dim MailDest As String 'email destination
        Dim Mess As String 'message
        Dim Subject As String 'subject
        Dim j As Long 'counter for times email appears
        Dim lRow As Long 'last Row of table
        Dim f As Range 'range for Find and FindNext loop
        Dim RangeToSearch As Range 'define the range of what to search
        Dim fFirst As Range 'fariable for first found range
        
        'use this to determine last row once instead of doing it multiple times within the loop
        lRow = WorksheetFunction.CountA(Columns(7))
        
        For Icounter = 2 To lRow
                                    
            'reset after each email
            Set outlookApp = CreateObject("outlook.application")
            Set OutlookMailItem = outlookApp.CreateItem(0)
            MailDest = ""
            Subject = ""
            Mess = ""
            j = 0
                
            If Cells(Icounter, 9) = "Captured" Then
                
                With OutlookMailItem
                    
                    'determine how many times emails with "captured" shows up
                    j = Application.CountIfs(Range("G2:G" & lRow), Cells(Icounter, 7), Range("I2:I" & lRow), Cells(Icounter, 9))
                    
                    If j = 1 Then
                        
                        Mess = Cells(Icounter, 4).Value & " - " & Cells(Icounter, 8).Value ' combined Cap and Mess into one instead of having two variables to manage
                        MailDest = Cells(Icounter, 7).Value
                        Subject = Cells(Icounter, 2).Value & "  " & Cells(Icounter, 4).Value & " Opportunities"
                        Cells(Icounter, 9) = "Sent to CSA"
                    
                    Else
                        
                        Set RangeToSearch = Range("G2:G" & lRow) 'define range to search
                        Set f = RangeToSearch.Find(Cells(Icounter, 7).Value, lookat:=xlPart, LookIn:=xlValues) 'set first find
                        Set fFirst = f 'set first address
                        
                        'maildest and subject stay the same
                        MailDest = Cells(f.Row, 7).Value
                        Subject = "New Capability Opportunities"
                        
                        'define first row of message and update "To send" column
                        Mess = Cells(f.Row, 4).Value & " - " & Cells(f.Row, 8).Value
                        
                        Cells(f.Row, 9) = "Sent to CSA"
                        
                        Do Until f Is Nothing
                            Set f = RangeToSearch.FindNext(f) 'set next find
                            If f.Address = fFirst.Address Then Exit Do 'exit loop criteria
                            
                            If Cells(f.Row, 9) = "Captured" Then
                                Mess = Mess & vbNewLine & Cells(f.Row, 4).Value & " - " & Cells(f.Row, 8).Value 'update mess
                                Cells(f.Row, 9) = "Sent to CSA" 'update To Send
                            End If
                        Loop
                    End If
                    
                    .To = MailDest 'not sure why you were using .cc instead of .to
                    .Subject = Subject
                    .Body = "List of Opportunities:" & _
                        vbNewLine & vbNewLine & Mess & vbNewLine & vbNewLine & _
                        "Refer to spreadsheet for complete list of all opportunities!"
                        'change to however you want email to be displayed, use vbnewline to enter new line
                        
                    .Display '.Send change to .send for automatic send, use .display when debugging
                End With
                
                Set OutlookMailItem = Nothing
                Set outlookApp = Nothing
                
            End If
            
        Next Icounter
        
    End Sub
    attached new file
    hopefully this one works properly now
    Attached Files Attached Files

  22. #22
    Registered User
    Join Date
    04-17-2016
    Location
    St Louis
    MS-Off Ver
    2013
    Posts
    13

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    I submitted my project Friday and everything works great! Thank you so much for your help!

  23. #23
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: EXCEL VBA send dynamic emails messages based on trigger column

    not a problem
    glad to have helped


    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel vba to auto-send customer emails (duplicate emails issue)
    By nadz84 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-29-2015, 10:08 AM
  2. Replies: 2
    Last Post: 12-19-2014, 11:28 AM
  3. Send Emails once expiry date is reached, and generate report based on emails sent
    By demonicscorpion in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-18-2014, 05:36 AM
  4. [SOLVED] Need help getting excel to send automatic emails based on cell value
    By Quito in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-11-2014, 07:09 AM
  5. Automatically send an email based on the value of a cell and/or trigger date
    By psn.arya in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-05-2013, 06:17 PM
  6. Send Emails based on cell value
    By kaki1956 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-22-2010, 08:28 PM
  7. Sending macro emails using excel: Send emails with their passwords.
    By loveisblind in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-12-2009, 03:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1