+ Reply to Thread
Results 1 to 11 of 11

Email rows if conditions met

Hybrid View

  1. #1
    Registered User
    Join Date
    12-14-2020
    Location
    Bahrain
    MS-Off Ver
    2010
    Posts
    14

    Email rows if conditions met

    I'm working in a vehicle workshop and I am trying to build a program that will email each service advisor via outlook, their outstanding jobs which have all parts available.
    (parts are ordered and the customer might take the car away, I want the advisors to be reminded daily so that they can get the customers back in for the parts to be fitted).

    My current sheet takes all of its information from our WIP handling system and is updated every morning automatically.

    Basically I am trying to set it so that IF date in column E is < TODAY AND Column D = All parts available AND column F = OUT THEN email the advisor in Column C all and only the rows that apply to that advisor.
    Then email the management team a summary of all jobs with parts in and vehicles out

    I have a sample code that i use on another sheet but all it does is email a link to that spreadsheet every morning, i want this one to me more targeted and actually email them rows from the spreadsheet individually.

    Is this possible?

    Many thanks.
    Attached Files Attached Files
    Last edited by adam1992; 01-12-2021 at 09:10 AM.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,844

    Re: Email rows if conditions met

    A few questions for clarification:
    -I don't see any email addresses on your sheet. Do you want the email address to remain blank in the email?
    -How do we know who the "managements team" is?

    Post the code you are currently using. Remember to use code tags when posting the code.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Email rows if conditions met

    Although @Mumps questions are relevant and answers are needed, I have made a number of assumptions to provide a possible solution. The code needs data entry in a few places, annotated by '''''''' characters. One assumption is that you can enter emails for each contact in column I as in the attached email. Also, enter the management email in cell M2.

    Sub Email_filter_todo()
    'Excel Macro : Filter and Paste Unique Values to New Sheets and new Email
    'This code writes to Range BB and then deletes that data
    'It creates new worksheets that are then deleted As well
    'It calls another code module to send summary to management
    Application.ScreenUpdating = False
    Dim x As Range
    Dim rng As Range
    Dim last, i, z, lr As Long
    Dim sht As String
    
    'specify sheet name in which the data is stored
    sht = "Sheet1"
    
    'change filter column in the following code
    last = Sheets(sht).Cells(Rows.Count, "C").End(xlUp).Row
    Set rng = Sheets(sht).Range("A1:I" & last)
    Sheets(sht).Range("I1:I" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("BB1"), Unique:=True
        For Each x In Range([BB2], Cells(Rows.Count, "BB").End(xlUp))
            With rng
                .AutoFilter
                .AutoFilter Field:=9, Criteria1:=x.Value
                .AutoFilter Field:=4, Criteria1:="All Parts available"
                .AutoFilter Field:=6, Criteria1:="OUT"
                .AutoFilter Field:=5, Criteria1:="<" & Date, Operator:=xlAnd
                If .SpecialCells(xlCellTypeVisible).Count > 10 Then
                    .SpecialCells(xlCellTypeVisible).Copy
                    Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
                    ActiveSheet.Paste
                    ActiveSheet.Columns.AutoFit
                    Columns("I").EntireColumn.Hidden = True
                    'Call code below
                    Send_newemail (i)
                    Columns("I").EntireColumn.Hidden = False
                    'Code Removes new sheet that was created
                    Application.DisplayAlerts = False
                        Sheets(x.Text).Delete
                    Application.DisplayAlerts = True
                End If
            End With
        Next x
    
        ' Turn off filter
        Sheets(sht).AutoFilterMode = False
        
        ' Remove data from column BB
        Range([BB2], Cells(Rows.Count, "BB").End(xlUp)) = ""
    
        With Application
            .CutCopyMode = False
            .ScreenUpdating = True
        End With
        
        'Run code from below to send summary to management
        Call Email_to_Management
    End Sub
    
    Sub Send_newemail(i)
        Dim OutApp As Object
        Dim OutMail As Object
        Dim rng As Range
        Dim lr, a, b, y As Integer
        Dim sht As String
    
        sht = "Sheet1"
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
       
        Set rng = Nothing
        On Error Resume Next
        Set rng = Selection.SpecialCells(xlCellTypeVisible)
        lr = Cells(Rows.Count, "I").End(xlUp).Row 'with cells = 3
        On Error GoTo 0
       
        With OutMail
            .To = Sheets(sht).Range("I2").Value
            .CC = "" '''''' Enter Value if needed, maybe your own email to confirm email sent
            .Subject = "Daily Customer Part Update" ' Range("C2").Value
            .HTMLBody = RangetoHTML(rng) & vbNewLine
            .Display
            '.Send ''''''Remove apostrophe in front of this line and add apostrophe to .display line to send email without seeing draft email
       End With
    
       Set OutMail = Nothing
       Set OutApp = Nothing
        
    End Sub
    
    Function RangetoHTML(rng As Range)
    'Function Required for the code to work correctly
        Dim FSO As Object
        Dim ts As Object
        Dim TempFile As String
        Dim TempWB As Workbook
    
        TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
    
        rng.Copy
        Set TempWB = Workbooks.Add(1)
        With TempWB.Sheets(1)
            .Cells(1).PasteSpecial Paste:=8
            .Cells(1).PasteSpecial xlPasteValues, , False, False
            .Cells(1).PasteSpecial xlPasteFormats, , False, False
            .Cells(1).Select
            Application.CutCopyMode = False
            On Error Resume Next
            .DrawingObjects.Visible = True
            .DrawingObjects.Delete
            On Error GoTo 0
        End With
        With TempWB.PublishObjects.Add( _
             SourceType:=xlSourceRange, _
             Filename:=TempFile, _
             Sheet:=TempWB.Sheets(1).Name, _
             Source:=TempWB.Sheets(1).UsedRange.Address, _
             HtmlType:=xlHtmlStatic)
            .Publish (True)
        End With
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set ts = FSO.GetFile(TempFile).OpenAsTextStream(1, -2)
        RangetoHTML = ts.ReadAll
        ts.Close
        RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                              "align=left x:publishsource=")
        TempWB.Close savechanges:=False
        Kill TempFile
        Set ts = Nothing
        Set FSO = Nothing
        Set TempWB = Nothing
    End Function
    
    Sub Email_to_Management()
    
    Application.ScreenUpdating = False
    Dim x As Range
    Dim rng As Range
    Dim last, i, z, lr As Long
    Dim sht As String
    
    'specify sheet name in which the data is stored
    sht = "Sheet1"
    
    'change filter column in the following code
    last = Sheets(sht).Cells(Rows.Count, "C").End(xlUp).Row
    Set rng = Sheets(sht).Range("A1:I" & last)
            With rng
                .AutoFilter
                .AutoFilter Field:=4, Criteria1:="All Parts available"
                .AutoFilter Field:=6, Criteria1:="OUT"
                .AutoFilter Field:=5, Criteria1:="<" & Date, Operator:=xlAnd
                If .SpecialCells(xlCellTypeVisible).Count > 10 Then
                    .SpecialCells(xlCellTypeVisible).Copy
                    Sheets.Add(After:=Sheets(Sheets.Count)).Name = "Sum"
                    ActiveSheet.Paste
                    ActiveSheet.Columns.AutoFit
                    'Call code below
                    Send_newemailM (i)
                    'Code Removes new sheet that was created
                    Application.DisplayAlerts = False
                        Sheets("Sum").Delete
                    Application.DisplayAlerts = True
                End If
            End With
    
        ' Turn off filter
        Sheets(sht).AutoFilterMode = False
    
        With Application
            .CutCopyMode = False
            .ScreenUpdating = True
        End With
    
    End Sub
    
    Sub Send_newemailM(i)
        Dim OutApp As Object
        Dim OutMail As Object
        Dim rng As Range
        Dim lr, a, b, y As Integer
        Dim sht As String
    
        sht = "Sheet1"
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
       
        Set rng = Nothing
        On Error Resume Next
        Set rng = Selection.SpecialCells(xlCellTypeVisible)
        lr = Cells(Rows.Count, "I").End(xlUp).Row 'with cells = 3
        On Error GoTo 0
       
        With OutMail
            .To = Sheets(sht).Range("M2").Value 
            .CC = "" '''''' Enter Value if needed, maybe your own email to confirm email sent
            .Subject = "Daily Customer Part Update Summary"
            .HTMLBody = RangetoHTML(rng) & vbNewLine
            .Display
            '.Send '''''' Remove apostrophe in front of this line and add apostrophe to .display line to send email without seeing draft email
       End With
    
       Set OutMail = Nothing
       Set OutApp = Nothing
        
    End Sub
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    12-14-2020
    Location
    Bahrain
    MS-Off Ver
    2010
    Posts
    14

    Re: Email rows if conditions met

    My apologies, I have updated the example with the emails. Column I displays an email based on an IF statement and the managers emails are in cell M2.

    I have played about with your code and it seems to work well, Thank you so much.

    I have a couple of questions though,

    If I leave the email cell blank, the code stops working. Because I don't want to send an email to everyone who has parts in and only the service advisors, is it possible to adapt the code to pass if it see's a blank cell?

    Is it possible to have the email sent automatically i.e, I'd like to send an update to the service advisors (all emails in column I) every morning but only send to the Management team on a Sunday morning (middle east so sunday is the first working day of the week).

    I would also like to send a full breakdown of spreadsheet to the commercial vehicle dept.
    Is it possible to do something like, IF B2 = "C" THEN email all lines that apply rather than just "all parts available"

    Thank you
    Attached Files Attached Files
    Last edited by adam1992; 01-09-2021 at 04:07 AM.

  5. #5
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Email rows if conditions met

    The code has been updated to deal with Blank email lines. I also added a button to execute the email summary to management. Reviewing your other requests.

    For
    I would also like to send a full breakdown of spreadsheet to the commercial vehicle dept.
    Do you want a button to send the table contents only when Column B contains a C?
    Attached Files Attached Files
    Last edited by maniacb; 01-09-2021 at 02:29 PM.

  6. #6
    Registered User
    Join Date
    12-14-2020
    Location
    Bahrain
    MS-Off Ver
    2010
    Posts
    14

    Re: Email rows if conditions met

    That's fantastic, thank you

    There's one issue though, when you click 'send email' it filters them correctly but wants to send each email to 'grthomas'
    The managers button though, works perfectly

    Yes, a button that basically filters column B to show only 'C' and then email the full sheet regardless of parts delivered or OUT etc.

    Sorry if I am being a pest, I am very new to using this function

  7. #7
    Registered User
    Join Date
    12-14-2020
    Location
    Bahrain
    MS-Off Ver
    2010
    Posts
    14

    Re: Email rows if conditions met

    Got the commercial vehicle bit working now, I just added another button and copied your code into another macro with different filters.

    Only issue remaining is the advisors button sending all the emails to the same person

  8. #8
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Email rows if conditions met

    Repaired the first two modules.

    Sub Email_filter_todo()
    'Excel Macro : Filter and Paste Unique Values to New Sheets and new Email
    'This code writes to Range BB and then deletes that data
    'It creates new worksheets that are then deleted As well
    'It calls another code module to send summary to management
    Application.ScreenUpdating = False
    Dim x As Range
    Dim rng As Range
    Dim last, i, z, lr As Long
    Dim sht As String
    
    'specify sheet name in which the data is stored
    sht = "Sheet1"
    
    'change filter column in the following code
    last = Sheets(sht).Cells(Rows.Count, "C").End(xlUp).Row
    Set rng = Sheets(sht).Range("A1:I" & last)
    Sheets(sht).Range("I1:I" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("BB1"), Unique:=True
    i = 1
        For Each x In Range([BB2], Cells(Rows.Count, "BB").End(xlUp))
        i = i + 1
            If x.Value = "" Then GoTo skip
            With rng
                .AutoFilter
                .AutoFilter Field:=9, Criteria1:=x.Value
                .AutoFilter Field:=4, Criteria1:="All Parts available"
                .AutoFilter Field:=6, Criteria1:="OUT"
                .AutoFilter Field:=5, Criteria1:="<" & Date, Operator:=xlAnd
                If .SpecialCells(xlCellTypeVisible).Count > 10 Then
                    .SpecialCells(xlCellTypeVisible).Copy
                    Sheets.Add(After:=Sheets(Sheets.Count)).Name = Left(x.Value, 4)
                    Sheets(Left(x.Text, 4)).Paste
                    Sheets(Left(x.Text, 4)).Columns.AutoFit
                    Sheets(Left(x.Text, 4)).Columns("I").EntireColumn.Hidden = True
                    'Call code below
                    Send_newemail (i)
                    'Columns("I").EntireColumn.Hidden = False
                    'Code Removes new sheet that was created
                    Application.DisplayAlerts = False
                        Sheets(Left(x.Text, 4)).Delete
                    Application.DisplayAlerts = True
                End If
            End With
    skip:
        Next x
    
        ' Turn off filter
        Sheets(sht).AutoFilterMode = False
        
        ' Remove data from column BB
        Range([BB2], Cells(Rows.Count, "BB").End(xlUp)) = ""
    
        With Application
            .CutCopyMode = False
            .ScreenUpdating = True
        End With
        
    End Sub
    
    Sub Send_newemail(i)
        Dim OutApp As Object
        Dim OutMail As Object
        Dim rng As Range
        Dim lr, a, b, y As Integer
        Dim sht As String
    
        sht = "Sheet1"
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
       
        Set rng = Nothing
        On Error Resume Next
        Set rng = Selection.SpecialCells(xlCellTypeVisible)
        lr = Cells(Rows.Count, "I").End(xlUp).Row 'with cells = 3
        On Error GoTo 0
       
        With OutMail
            .To = Sheets(sht).Cells(i, "I").Value
            .CC = "" '''''' Enter Value if needed, maybe your own email to confirm email sent
            .Subject = "Daily Customer Part Update" ' Range("C2").Value
            .HTMLBody = RangetoHTML(rng) & vbNewLine
            .Display
            '.Send ''''''Remove apostrophe in front of this line and add apostrophe to .display line to send email without seeing draft email
       End With
    
       Set OutMail = Nothing
       Set OutApp = Nothing
        
    End Sub

  9. #9
    Registered User
    Join Date
    12-14-2020
    Location
    Bahrain
    MS-Off Ver
    2010
    Posts
    14

    Re: Email rows if conditions met

    Thank you,
    Now the grthomas and sajeesh.s emails are working fine, but the next one tries to send the sheet for jaffar.taher to hamza and the remaining sheets don't have a recipient

    I tried changing the range from "BB" to "I" but it tried to send 300 seperate emails

  10. #10
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Email rows if conditions met

    my bad, I was overthinking the process. This correction should resolve the issue;

    Sub Email_filter_todo()
    'Excel Macro : Filter and Paste Unique Values to New Sheets and new Email
    'This code writes to Range BB and then deletes that data
    'It creates new worksheets that are then deleted As well
    'It calls another code module to send summary to management
    Application.ScreenUpdating = False
    Dim x As Range
    Dim rng As Range
    Dim last, i, z, lr As Long
    Dim sht As String
    
    'specify sheet name in which the data is stored
    sht = "Sheet1"
    
    'change filter column in the following code
    last = Sheets(sht).Cells(Rows.Count, "C").End(xlUp).Row
    Set rng = Sheets(sht).Range("A1:I" & last)
    Sheets(sht).Range("I1:I" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("BB1"), Unique:=True
    i = 1
        For Each x In Range([BB2], Cells(Rows.Count, "BB").End(xlUp))
        i = i + 1
            If x.Value = "" Then GoTo skip
            With rng
                .AutoFilter
                .AutoFilter Field:=9, Criteria1:=x.Value
                .AutoFilter Field:=4, Criteria1:="All Parts available"
                .AutoFilter Field:=6, Criteria1:="OUT"
                .AutoFilter Field:=5, Criteria1:="<" & Date, Operator:=xlAnd
                If .SpecialCells(xlCellTypeVisible).Count > 10 Then
                    .SpecialCells(xlCellTypeVisible).Copy
                    Sheets.Add(After:=Sheets(Sheets.Count)).Name = Left(x.Value, 4)
                    Sheets(Left(x.Text, 4)).Paste
                    Sheets(Left(x.Text, 4)).Columns.AutoFit
                    Sheets(Left(x.Text, 4)).Columns("I").EntireColumn.Hidden = True
                    'Call code below
                    Send_newemail
                    'Columns("I").EntireColumn.Hidden = False
                    'Code Removes new sheet that was created
                    Application.DisplayAlerts = False
                        Sheets(Left(x.Text, 4)).Delete
                    Application.DisplayAlerts = True
                End If
            End With
    skip:
        Next x
    
        ' Turn off filter
        Sheets(sht).AutoFilterMode = False
        
        ' Remove data from column BB
        Range([BB2], Cells(Rows.Count, "BB").End(xlUp)) = ""
    
        With Application
            .CutCopyMode = False
            .ScreenUpdating = True
        End With
        
    End Sub
    
    Sub Send_newemail()
        Dim OutApp As Object
        Dim OutMail As Object
        Dim rng As Range
        Dim lr, a, b, y As Integer
        Dim sht As String
    
        sht = "Sheet1"
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
       
        Set rng = Nothing
        On Error Resume Next
        Set rng = Selection.SpecialCells(xlCellTypeVisible)
        lr = Cells(Rows.Count, "I").End(xlUp).Row 'with cells = 3
        On Error GoTo 0
       
        With OutMail
            .To = Range("I2").Value
            .CC = "" '''''' Enter Value if needed, maybe your own email to confirm email sent
            .Subject = "Daily Customer Part Update" ' Range("C2").Value
            .HTMLBody = RangetoHTML(rng) & vbNewLine
            .Display
            '.Send ''''''Remove apostrophe in front of this line and add apostrophe to .display line to send email without seeing draft email
       End With
    
       Set OutMail = Nothing
       Set OutApp = Nothing
        
    End Sub

  11. #11
    Registered User
    Join Date
    12-14-2020
    Location
    Bahrain
    MS-Off Ver
    2010
    Posts
    14

    Re: Email rows if conditions met

    Perfect now, thank you for your help

+ 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] Email range macro shows hidden rows in the email
    By jennis7242 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-09-2019, 11:04 AM
  2. [SOLVED] Help combining macros to delete rows on conditions and add data into cells on conditions
    By JayJayGC in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 10-03-2017, 10:14 AM
  3. email notification by excel with some specified conditions
    By kumaraguru in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-19-2016, 11:46 PM
  4. Email Reminder for Highlighted Rows & Email Addresses
    By xerksis in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-07-2015, 02:46 AM
  5. [SOLVED] Re: Loop through the rows and extract unique email id's to generate email drafts
    By spiwere in forum Excel Programming / VBA / Macros
    Replies: 28
    Last Post: 01-28-2014, 04:12 AM
  6. email if conditions are meet
    By keithnrhonda in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-08-2014, 01:13 AM
  7. Excel email with three conditions
    By CindyG in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-06-2009, 02:12 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