+ Reply to Thread
Results 1 to 7 of 7

Send an Email when a Cell Contains the Desired Text

Hybrid View

tomprestriidge Send an Email when a Cell... 12-10-2018, 06:00 AM
PaulSP8 Re: Send an Email when a Cell... 12-10-2018, 07:02 AM
tomprestriidge Re: Send an Email when a Cell... 12-10-2018, 07:23 AM
PaulSP8 Re: Send an Email when a Cell... 12-10-2018, 07:51 AM
tomprestriidge Re: Send an Email when a Cell... 12-10-2018, 07:59 AM
PaulSP8 Re: Send an Email when a Cell... 12-10-2018, 08:43 AM
tomprestriidge Re: Send an Email when a Cell... 12-10-2018, 09:14 AM
  1. #1
    Registered User
    Join Date
    08-14-2018
    Location
    UK
    MS-Off Ver
    2013 (Work), 365 (Home)
    Posts
    17

    Question Send an Email when a Cell Contains the Desired Text

    Hi all, TIA for any help.

    I have a cell that contains a drop down with two options; Accept or Reject. When Accept is selected, I would like an Outlook email window to pop up ready to send, and the same for Reject. The emails will only contain different text bodies.

    Any help is appreciated.

  2. #2
    Valued Forum Contributor PaulSP8's Avatar
    Join Date
    11-21-2017
    Location
    Chorley, England
    MS-Off Ver
    2013 & 2021
    Posts
    437

    Re: Send an Email when a Cell Contains the Desired Text

    You've not gone into any detail so I've set the Drop Down box as A1

    Can you work with this?

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Address = "$A$1" Then
    
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        If Range("A1") = "Accept" Then
            strbody = "Hi there" & vbNewLine & vbNewLine & _
                    "Accepted!" & vbNewLine
            strSubject = "Accepted!"
        End If
        
        If Range("A1") = "Reject" Then
            strbody = "Hi there" & vbNewLine & vbNewLine & _
                    "Rejected!" & vbNewLine
            strSubject = "Rejected!"
        End If
    
        If Range("A1") <> "Accept" And Range("A1") <> "Reject" Then Exit Sub
    
        On Error Resume Next
        With OutMail
            .To = "some.email@address.com"
            .CC = ""
            .BCC = ""
            .Subject = strSubject
            .Body = strbody
            .Display
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    End If
    
    End Sub

  3. #3
    Registered User
    Join Date
    08-14-2018
    Location
    UK
    MS-Off Ver
    2013 (Work), 365 (Home)
    Posts
    17

    Re: Send an Email when a Cell Contains the Desired Text

    Thank you for your help.

    The cell that contains the dropdown is G4. However, the table will often add new rows, each of which will have the Accept/Reject drop down cell (G5, G6, G7 etc. etc.). So can the code recognise this or will something need to be changed?

  4. #4
    Valued Forum Contributor PaulSP8's Avatar
    Join Date
    11-21-2017
    Location
    Chorley, England
    MS-Off Ver
    2013 & 2021
    Posts
    437

    Re: Send an Email when a Cell Contains the Desired Text

    Try;

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Column = 7 Then
    
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
        If Cells(Target.Row, Target.Column) = "Accept" Then
            strbody = "Hi there" & vbNewLine & vbNewLine & _
                    "Accepted!" & vbNewLine
            strSubject = "Accepted!"
        End If
        
        If Cells(Target.Row, Target.Column) = "Reject" Then
            strbody = "Hi there" & vbNewLine & vbNewLine & _
                    "Rejected!" & vbNewLine
            strSubject = "Rejected!"
        End If
    
        If Cells(Target.Row, Target.Column) <> "Accept" And Cells(Target.Row, Target.Column) <> "Reject" Then Exit Sub
    
        On Error Resume Next
        With OutMail
            .To = "some.email@address.com"
            .CC = ""
            .BCC = ""
            .Subject = strSubject
            .Body = strbody
            .Display
        End With
        On Error GoTo 0
    
        Set OutMail = Nothing
        Set OutApp = Nothing
    
    
    End If
    
    End Sub

  5. #5
    Registered User
    Join Date
    08-14-2018
    Location
    UK
    MS-Off Ver
    2013 (Work), 365 (Home)
    Posts
    17

    Re: Send an Email when a Cell Contains the Desired Text

    This is perfect! Thank you very much for your help.

    Am I correct in saying that I can use the Target.Offset(Y, X) to pick out values from the row to then use in the email body?

  6. #6
    Valued Forum Contributor PaulSP8's Avatar
    Join Date
    11-21-2017
    Location
    Chorley, England
    MS-Off Ver
    2013 & 2021
    Posts
    437

    Re: Send an Email when a Cell Contains the Desired Text

    No worries at all, glad to help.

    Yes you can do that,

    If Target.Column = 7 Then
    
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
        If Cells(Target.Row, Target.Column) = "Accept" Then
            strbody = "Hi there" & vbNewLine & vbNewLine & _
                    Target.Offset(0, 1).Value & vbNewLine
            strSubject = "Accepted!"
        End If

    Or I'd do it like this if I'm honest to keep it more "readable";

    If Target.Column = 7 Then
    
        ARRow = Target.Row
    
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        
        If Cells(Target.Row, Target.Column) = "Accept" Then
            strbody = "Hi there" & vbNewLine & vbNewLine & _
                    Cells(ARRow, "H").Value & vbNewLine
            strSubject = "Accepted!"
        End If
    The choice is yours, whatever you feel comfortable with like anything else

  7. #7
    Registered User
    Join Date
    08-14-2018
    Location
    UK
    MS-Off Ver
    2013 (Work), 365 (Home)
    Posts
    17

    Re: Send an Email when a Cell Contains the Desired Text

    Brilliant, cheers!

+ 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. [SOLVED] Send email if cell contains specific text
    By smudgers9 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 01-22-2018, 10:17 AM
  2. Excel to auto send email based on the text of a cell
    By MTC2016 in forum Excel General
    Replies: 4
    Last Post: 01-11-2017, 01:50 PM
  3. [SOLVED] VBA to send email using data from the EXCEL including email address, subject and body text
    By ec4excel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-18-2016, 11:07 AM
  4. Have Macro Send Email Only When Cell Contains Text Active
    By onelifestyle in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-26-2015, 04:14 PM
  5. Send an Email by cell date in comparison to actual date referencing cell text.
    By greaseebogus in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-15-2014, 10:03 PM
  6. Workflow with signatures (if cell=x then send email, if signed then send email)
    By Kate2811 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-25-2014, 05:37 AM
  7. Unable to send user-editable text to Outlook email body based on active cell.
    By m0meehan in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-20-2014, 01:51 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