Email with rows included in VBA with additional criteria

peapop

New Member
Joined
Feb 20, 2022
Messages
6
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi all,

I previously had sought help in this forum on how to create a macro that generate email(s) that add rows when specific text ("Critical") is found on column AF. Here are a few screenshots on how it looks like;
1646073072649.png



1646073043815.png


I would like to indicate a "Yes" in Column "AG" for those rows that are already processed by the macro, so when the macro is run the second time, it will prevent those rows of data which contain "Yes" in column AG to be added in the email.

1646073264389.png

1646073284999.png


This is the excellent code that was originally posted in the prev post
VBA Code:
Option Explicit

Sub OrderCritical()     'Give your macro a meaningful name

' Macro to request quotes for all critical stock
 
    Dim vInput As Variant
    Dim lNmeRow As Long, lR As Long, UB1 As Long, UB2 As Long, lName As Long, _
        lEmailAdr As Long, lCatNr As Long, lAlert As Long, lQR As Long, lHeadrRow As Long, lC As Long
    Dim MailTo As String, MailSubject As String, MailBody As String, sAddRow As String, sHead As String, _
        sTableHdr As String
    Dim OutApp As Object, OutMail As Object
   
   
    'Check if  Outlook already opened
    Set OutApp = GetObject(Class:="Outlook.Application")

    If OutApp Is Nothing Then
        'Outlook is not opened, so open
        Set OutApp = CreateObject("Outlook.Application")
    End If
   
    'find header row
    lHeadrRow = Range("C:C").Find("Name").Row
    'Put database into array
    vInput = Range("C" & lHeadrRow).CurrentRegion.Value
   
    'number of rows in database:
    UB1 = UBound(vInput, 1)
    'number of columns in database:
    UB2 = UBound(vInput, 2)
   
    'Find columns to be used
    For lC = 1 To UB2
        sHead = vInput(1, lC)
        Select Case True
            Case sHead Like "Name*"
                lName = lC
            Case sHead Like "*Vendor*"
                lEmailAdr = lC
            Case sHead Like "Catalo*"
                lCatNr = lC
            Case sHead Like "Alert*"
                lAlert = lC
            Case sHead Like "Quotat*"
                lQR = lC
               
        End Select
    Next lC

    ' >>>> MailSubject does not change, so only needs to be created once, outside loop
    MailSubject = "Quotation Request"
         
 
    'Create the html table and header from the first row
    sTableHdr = "<table border=1><tr><th>" & vInput(1, lName) & "</th>" _
            & "<th>" & vInput(1, lCatNr) & "</th>" _
            & "<th>" & "Quantity" & "</th>" _

    'Check to see if column lAlert (AF) = 'SAFE' and skip mail if it does
    For lR = 2 To UB1
       
        If vInput(lR, lAlert) Like "CRITICAL" And Not vInput(lR, lQR) = True Then    'True is flag to indicate item already processed
           
            MailTo = vInput(lR, lEmailAdr)
           
            'Create MailBody table row for first row
            MailBody = "<tr>" _
                    & "<td>" & vInput(lR, lName) & "</td>" _
                    & "<td>" & vInput(lR, lCatNr) & "</td>" _
                    & "</tr>"
            'set flag that line is processed
            vInput(lR, lQR) = True
       
            'Second loop checks all critical items from the same vendor.
            For lC = lR + 1 To UB1
               
                If MailTo Like vInput(lC, lEmailAdr) And vInput(lC, lAlert) Like "CRITICAL" Then
                       
                    'Create additional table row for each extra row found"
                    sAddRow = "<tr>" _
                            & "<td>" & vInput(lC, lName) & "</td>" _
                            & "<td>" & vInput(lC, lCatNr) & "</td>" _
                            & "</tr>"
                   
                    MailBody = MailBody & sAddRow  '
                       
                    vInput(lC, lQR) = True
                       
                End If
            Next lC
           
            ' Now create email
            Set OutMail = OutApp.createitem(0)
            With OutMail
                 .To = MailTo
                 .Subject = MailSubject
                 .HTMLBody = sTableHdr & MailBody & "</table>"
                 .Display
             'send
            End With
                       
        End If
    Next lR
   
End Sub
 

Attachments

  • 1646072582719.png
    1646072582719.png
    24.2 KB · Views: 26
  • 1646072867182.png
    1646072867182.png
    45.5 KB · Views: 16

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,223,966
Messages
6,175,662
Members
452,666
Latest member
AllexDee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top