Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29755

Using Excel VBA IF THEN statment in the body of an email with "2" THEN results

$
0
0

What I am looking for is when: cell("D").Value = "4. Confirmed" Then "The text" but also further down in the .body of the email if .Value = "4. Confirmed" it will add the additional sentence.

Sub Email_From_Excel_Basic()

' This identifies what means what
    Dim emailApplication As Object
    Dim emailItem As Object
    Dim mymsg As String
    Dim cell As Range

    Application.ScreenUpdating = False
    Set emailApplication = CreateObject("Outlook.Application")

' Now we build the email.
    On Error GoTo cleanup
    For Each cell In Worksheets("owvr").Columns("S").Cells
        Set emailItem = emailApplication.CreateItem(0)
        If cell.Value Like "?*@?*.?*" And _
           Cells(cell.Row, "T") = "Yes" Then
            With emailItem
' This is who the email is being sent to
                .To = Cells(cell.Row, "S").Value
                .CC = Cells(cell.Row, "S").Value
' This is the subject of the email
                .Subject = "Status update"' This is the body of the email based on cell references
                mymsg = "Dear "& Cells(cell.Row, "A").Value & " team,"& vbNewLine & vbNewLine
                Dim sts As String
                If Cells(cell.Row, 4).Value = "1. New Order" Then
                    sts = "Your order has been received and will be processed."
                ElseIf Cells(cell.Row, 4).Value = "2. Shipped" Then
                    sts = "Your order has been shipped"
                ElseIf cell("D").Value = "3. In-Process" Then
                    sts = "Your order has been received. We are waiting on information to confirm your order."
                ElseIf cell("D").Value = "4. Confirmed" Then
                    sts = "Your order has been confirmed. We will approve your order to ship as all requirements are fulfilled."
                ElseIf cell("D").Value = "5. Approved" Then
                    sts = "Your order is approved to ship."
                Else
                End If
'Then we continue into the rest of the body
                mymsg = mymsg & "Status: "& stts & vbNewLine
                mymsg = mymsg & "number: "& Cells(cell.Row, "I").Value & vbNewLine
                mymsg = mymsg & "type: "& Cells(cell.Row, "C").Value & vbNewLine
                mymsg = mymsg & "number: "& Cells(cell.Row, "B").Value & vbNewLine
                mymsg = mymsg & "Incoterms: "& Cells(cell.Row, "P").Value & vbNewLine
                mymsg = mymsg & "EXW: "& Cells(cell.Row, "E").Value & vbNewLine
                mymsg = mymsg & "delivery: "& Cells(cell.Row, "H").Value & vbNewLine & vbNewLine

The issue comes in here, with what I have it will not send out an email if the Value = "4. Confirmed". But it will send the others.

            If Cells(cell.Row, 4).Value = "4. Confirmed" Then
                sts = "Deposit invoice: "& Cells(cell.Row, "K").Value & vbNewLine & "Additional invoice: "& Cells(cell.Row, "M").Value & vbNewLine & "Insurance certificate: "& Cells(cell.Row, "J").Value & vbNewLine & "Broker/Freight forwarder information: "& Cells(cell.Row, "L").Value & vbNewLine
            Else
            End If
            mymsg = mymsg & "For further details please use the contact information below:"& vbNewLine & vbNewLine
            mymsg = mymsg & "Best regards"& vbNewLine & vbNewLine
            .Body = mymsg
            .Send
        End With
        Set emailItem = Nothing
    End If
Next cell
cleanup:
    Set emailApplication = Nothing
    Application.ScreenUpdating = True
End Sub

Viewing all articles
Browse latest Browse all 29755

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>