+ Reply to Thread
Results 1 to 2 of 2

send email based on cell value

Hybrid View

Jamesraywebber85 send email based on cell value 12-19-2018, 12:44 PM
6StringJazzer Re: send email based on cell... 12-19-2018, 03:19 PM
  1. #1
    Forum Contributor
    Join Date
    02-12-2018
    Location
    Clearwater, FL
    MS-Off Ver
    MS 365
    Posts
    217

    send email based on cell value

    I have this code here:
    Sub send_Email()
        
        Dim i As Long
        Dim h As Long
        Dim OutApp, OutMail As Object
        Dim strto, strcc, strsub, strbody As String
        Dim sh As Worksheet
        Dim signature As String
        Dim msg As String
        Dim Phone As String
        Dim Warehouse As Range
        Dim WH As String
        
        
        Set OutApp = CreateObject("Outlook.Application")
        OutApp.Session.Logon
       
       Set sh = Sheets("Master Orders")
       
        For i = 2 To Range("C50000").End(xlUp).Row
            If Cells(i, 21) = "Yes" Then
        Select Case Time
            Case Is < TimeValue("12:00")
                msg = "Good morning " & Cells(i, 5)
            Case Is < TimeValue("16:00")
                msg = "Good afternoon " & Cells(i, 5)
            Case Else
                msg = "Good evening " & Cells(i, 5)
        End Select
           
        
        For Each Warehouse In Worksheets("Customer Data").Cells(i, 72)
        
    If Warehouse = "Dartmouth" Then WH = Worksheets("Codes").Range("P1:V1").Value
    If Warehouse = "Delta" Then WH = Worksheets("Codes").Range("P2:V2").Value
    If Warehouse = "Winnipeg" Then WH = Worksheets("Codes").Range("P3:V3").Value
    If Warehouse = "Mississauga" Then WH = Worksheets("Codes").Range("P4:V4").Value
    If Warehouse = "Laval" Then WH = Worksheets("Codes").Range("P5:V5").Value
    
    Next
                    Set OutMail = OutApp.CreateItem(0)
                    strto = Cells(i, 7).Value 'email address
                    strcc = "kchihani@pods.com"
                    strsub = "Clear the container at bond - Bonded Warehouse in " & Worksheets("Customer Data").Cells(i, 74)   'email subject
                    strbody = "<p style='font-family:calibri;font-size:14'>" & msg & "<BR><BR>" & "This message is to inform you that the container has reached the bonded warehouse located:" & "</STYLE></FONT>" & _
                    "<p style='font-family:calibri;font-size:14;Bold'>" & WH & "</Style></Font><BR>" & "<p style='font-family:New Times Roman;font-size:16'>" & Warehouse & "<BR><BR>" & "</style></font>" & "You may give them a call at " & Phone & ".  Please ask for a document called 'cargo control' and take that document, your inventory list and passport to the closest customs office. The customs agent will revise your documents and will ask you to please return the cargo control document back to the bonded facility to complete the process. Once the document is returned and the container is released, we can complete your transport to the final destination storage center.  Furthermore, we can move forward with scheduling your remaining moves to your destination address." < BR <> BR > "Thank you" 'email body
                    
                    
                    With OutMail
                    .Display
                    End With
                    signature = OutMail.HTMLbody
                    With OutMail
                        .To = strto
                        .CC = strcc
                        .Subject = strsub
                        .HTMLbody = strbody & vbNewLine & signature
                        '.send
                        .Display
                                        
                    End With
    
          
         
        Set OutMail = Nothing
        Set OutApp = Nothing
        Exit For
        Exit For
        
    End If
    
    Next
    
    End Sub
    I want it to look in the column on sheet("Customer Data") Column (BV) & find the City name. When the email generates then WH as the range from worksheet("Codes").Range(P:V) based on the city value located on worksheets("codes").Column(S). I ran the code but it does not throw an error & outlook does not generate the email. Any help would be greatly appreciated!

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,779

    Re: send email based on cell value

    I see a number of problems in your code, but I am not sure which one is keeping your emails from being created.

    I cannot run your code without your file and some sample data. Feel free to attach a file if it does not contain private data.

    Here are the things that need to be fixed:

    What module is this code in? If you have it in a standard module (e.g., Module1) all range references will default to be whatever is the active sheet. You haven't given enough information for me to determine if that's what you want. What sheet do you mean to refer to

        Dim strto, strcc, strsub, strbody As String
    I am sure you think you have declared four String variables. However, you have declared three Variant variables and one String variable. You must specify a data type for each variable when you have multiple variables on the same Dim statement.
        Dim strto As String, strcc As String, strsub As String, strbody As String
    You have not declared BR.

    You set sh
       Set sh = Sheets("Master Orders")
    but you never use it anywhere.

    You are setting your greeting message "Good morning" etc., each time through the loop. You can do this one time before entering the loop, since it's going to be the same greeting for every email.

    You are creating a loop, but referring only one cell. You don't need a loop to do this:
        For Each Warehouse In Worksheets("Customer Data").Cells(i, 72)
        
    If Warehouse = "Dartmouth" Then WH = Worksheets("Codes").Range("P1:V1").Value
    If Warehouse = "Delta" Then WH = Worksheets("Codes").Range("P2:V2").Value
    If Warehouse = "Winnipeg" Then WH = Worksheets("Codes").Range("P3:V3").Value
    If Warehouse = "Mississauga" Then WH = Worksheets("Codes").Range("P4:V4").Value
    If Warehouse = "Laval" Then WH = Worksheets("Codes").Range("P5:V5").Value
    
    Next
    Your If statements are mutually exclusive--you can only pick one. In such a case I would use a Case statement instead.

    You are setting Warehouse, a String variable, to a range of values. This will not work the way you are expecting. It will only use the first value in the range. Are you trying to create a string that concatenates all the cells from column P to V?

    I think you want something like this:
    Dim R As Long
    Dim Col as long
    
    Select Case Worksheets("Customer Data").Cells(i, 72)
    
       Case "Dartmouth"
          R = 1
       Case "Delta"
          R = 2
       Case "Winnipeg"
          R = 3
       Case "Mississauga"
          R = 4
       Case "Laval" 
          R = 5
    
    End Select
    
    For Col = 16 To 22
       WH = WH & " " & Worksheets("Codes").Cells(R, Col).Value
    Next Col
    You don't need to .Display twice. It's not harmful, but the first one is not useful.
                    With OutMail
                    .Display
                    End With
                    signature = OutMail.HTMLbody
                    With OutMail
                        .To = strto
                        .CC = strcc
                        .Subject = strsub
                        .HTMLbody = strbody & vbNewLine & signature
                        '.send
                        .Display
                                        
                    End With
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ 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. Send Email Based on Cell Value
    By M5B in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-13-2018, 02:07 PM
  2. [SOLVED] Send Email Based on Cell Values
    By weeblegobble in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-19-2018, 12:18 AM
  3. Can I have Excel send an email based on cell contents
    By msche09 in forum Excel General
    Replies: 2
    Last Post: 04-09-2014, 03:51 AM
  4. 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
  5. Send email to address based on cell value
    By shiftyspina in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-12-2014, 03:06 PM
  6. Send email based on address in a cell
    By behnam in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-19-2013, 02:53 PM
  7. send an email based on data in specific cell
    By savage in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-28-2010, 11:54 AM

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