learningthisstuff
learningthisstuff

Reputation: 101

Create Emails from Excel Loop

I have this sample sheet:
SheetSample

My code currently goes through and creates emails based on the name in column H. So Approver1 gets one email for all his people. I have gotten it to de-dupe any repeats of their employee names. Example: Approver 1 gets an email that says 'please approve time for all of your employees below:' and then there is a list of names...Sample1, Sample2, and Sample3. The sheet will often have dupe employees for each approver, as shown in my sheet above.

The code works well for the first set of dupe names (there could be up to 10 of the same Approvers in a row, all getting one email), then runs fine through any singles.

When it hits the next set of repeated approvers it skips the first row in that group, then creates emails for every other division; so it skips a row until it gets to the end of the dupe approver section. So from the sheet, approver1 would get his email all set, then approver2 would get hers, but then approver3 becomes a mess.

How do I get this to loop correctly through an entire list, creating one email for each approver, with all the corresponding names of their people listed only once?

Sub DivisionApprovals()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell, lookrng As Range
    Dim strdir As String
    Dim strFilename As String
    Dim sigString As String
    Dim strBody As String
    Dim strName As Variant
    Dim strName1 As Variant
    Dim strDept As Variant
    Dim strName2 As String
    Dim strbody2 As String
    Dim strName3 As Variant

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

    Set rng = ActiveSheet.UsedRange

    r = 2

    Do While r <= rng.rows.count
        Set OutMail = OutApp.CreateItem(0)

        Set strName = rng.Cells(r, 1)
        Set strName3 = rng.Cells(r, 3)
        strName2 = Trim(Split(strName, ",")(1))

        strBody = "<Font Face=calibri>Dear " & strName2 & ", <br><br> Please approve the following divisions:<br><br>"

        With OutMail
            .To = rng.Cells(r, 2).Value
            .Subject = "Please Approve Divisions"
            List = strName3 & "<br>"


            Do While rng.Cells(r, 1).Value = rng.Cells(r + 1, 1)
                r = r + 1
                Set strDept = rng.Cells(r, 3)
                .Subject = "Approvals Needed!"
                List = .HTMLBody & strDept & "<br>"
                r = r + 1
                .HTMLBody = List
            Loop
            .HTMLBody = strBody & "<B>" & List & "</B>" & "<br>" & Signature
            .Display

        End With

        Set OutMail = Nothing
        r = r + 1
    Loop
    Set OutApp = Nothing
End Sub

Upvotes: 4

Views: 3884

Answers (5)

Wookies-Will-Code
Wookies-Will-Code

Reputation: 735

I deleted the previous answer, then un-deleted it in case you need that info. So as to not confuse anyone, the answer building from the OP's code is below.

DISCLAIMER: I am not a fan of the incrementing code style in the Do While, it make sit very difficult to chase errors but I understand the intention. I have included code below this in the way that my brain works and perhaps better coding style, you be the judge.

Alright @learningthisstuff I figured out what was going on, the code assumes the names are sorted. One thing not provided for is if the dept names are the same it will be listed multiple times, are the dept always unique for a person (no dupes?) if there are dupes that is different code.

This code works I just ran it as a macro on a dummy set. Big thing was the sort AND the incrementing logic, I changed a few things to make it more readable/understandable along the way.

I hope this helps you and you can modify as things change for you.

Sub Email_Macro()
'
' Email_Macro Macro
'
    Dim OutApp As Object
    Dim OutMail As Object
    Dim cell, lookrng As Range
    Dim strdir As String
    Dim strFilename As String
    Dim sigString As String
    Dim strBody As String
    Dim strName As Variant
    Dim strName1 As Variant
    Dim strDept As Variant
    Dim strName2 As String
    Dim strbody2 As String
    Dim strName3 As Variant
    Dim emailWS As Worksheet
    Dim nameCol As Double
    Dim deptCol As Double
    Dim lastRow As Double
    Dim startRow As Double
    Dim r As Double

    Dim depList As String
    deptList = ""


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


    Set emailWS = ThisWorkbook.ActiveSheet
    startRow = 2 ' starting row
    nameCol = 1 'col of name
    deptCol = 3 'col of dept

    'find the last row with a name in it from the name column
    lastRow = emailWS.Cells(emailWS.Rows.Count, nameCol).End(xlUp).Row

    'set variable to the starting row #
    r = startRow 'this is where the counting begins

    'sort the data first before going through the email process
    'assumes these are the only columns 1 (nameCol) thru 3 (deptCol) to sort
    'assumes you are sorting based on col 1 (nameCol)
    emailWS.Range(Cells(startRow, nameCol), Cells(lastRow, deptCol)).Sort key1:=emailWS.Range(Cells(startRow, nameCol), Cells(lastRow, nameCol))

    Do While r <= lastRow
        Set OutMail = OutApp.CreateItem(0)

        Set strName = emailWS.Cells(r, nameCol)
        Set strName3 = emailWS.Cells(r, deptCol)
        'careful the line below assumes there is always a comma separator in the name
        strName2 = Trim(Split(strName, ",")(1))

        strBody = "<Font Face=calibri>Dear " & strName2 & ", <br><br> Please approve the following divisions:<br><br>"

        With OutMail
            .To = emailWS.Cells(r, 2).Value
            .Subject = "Please Approve Divisions"
            deptList = strName3 & "<br>"


            Do While emailWS.Cells(r, 1).Value = emailWS.Cells(r + 1, 1)
                r = r + 1
                Set strDept = emailWS.Cells(r, 3)
                .Subject = "Approvals Needed!"
                deptList = deptList & strDept & "<br>"
            Loop
            .HTMLBody = strBody & "<B>" & deptList & "</B>" & "<br>" & Signature
            .Display

        End With

        Set OutMail = Nothing

        'conditionally increment the row based on the name difference
        If emailWS.Cells(r, 1).Value <> emailWS.Cells(r + 1, 1) Then
            r = r + 1 'increment if there is a new name or no name
            deptList = "" 'reset the department list
        Else 'Do nothing
        End If
    Loop
    Set OutApp = Nothing


End Sub

Screenshot:

enter image description here

To prove that I don't throw out comments without backing it up with some solution / mentoring? This is much easier for me to understand and troubleshoot. It steps through the rows in a very predictable fashion and we handle each row based on specified conditions. I also try and use variable names that will let you know what they are for.

Sub Email_Macro()
'
' Email_Macro Macro
'
    Dim OutApp As Object 'email application
    Dim OutMail As Object 'email object
    Dim strBody As String 'first line of email body
    Dim strName As String 'name in the cell we are processing
    Dim strDept As String 'dept of the name we are processing
    Dim previousName As String 'previous name processed
    Dim nextName As String 'next name to process

    Dim emailWS As Worksheet 'the worksheet selected wehn running macro
    Dim nameCol As Double 'column # of names
    Dim deptCol As Double 'column # of depts
    Dim lastRow As Double 'last row of data in column
    Dim startRow As Double 'row we wish to start processing on
    Dim r As Double 'loop variable for row
    'This will be the list of departments, we will build it as we go
    Dim depList As String
    Dim strSig As String 'email signature
    strSig = "Respectfully, <br> Wookie"

    deptList = "" 'empty intitialization
    previousName = "" 'empty intialization
    nextName = "" 'empty intialization

    'Turn off screen updating
    'Application.ScreenUpdating = False
    'choose email application
    Set OutApp = CreateObject("Outlook.Application")
    'set worksheet to work on as active (selected sheet)
    Set emailWS = ThisWorkbook.ActiveSheet
    startRow = 2 ' starting row
    nameCol = 1 'col of names, can also do nameCol = emailWS.Range("A1").Column
    deptCol = 3 'col of depts, can also do deptCol = emailWS.Range("A3").Column
    '** Advantage of the optional way is if you have many columns and you don't want to count them

    'find the last row with a name in it from the name column
    lastRow = emailWS.Cells(emailWS.Rows.Count, nameCol).End(xlUp).Row

    'sort the data first before going through the email process using Range sort and a key
    'assumes these are the only columns 1 (nameCol) thru 3 (deptCol) to sort
    'assumes you are sorting based on col 1 (nameCol)
    emailWS.Range(Cells(startRow, nameCol), Cells(lastRow, deptCol)).Sort key1:=emailWS.Range(Cells(startRow, nameCol), Cells(lastRow, nameCol))

    'Set up our loop, it will go through every cell in the column we select in the loop
    For r = startRow To lastRow
        'Get the name and dept
        'For the name we will split around the comma and take the second part of array (right of comma)
        strName = Trim(Split(emailWS.Cells(r, nameCol), ",")(1))
        strDept = emailWS.Cells(r, deptCol)

        'if the next name is not blank (EOF)
        If emailWS.Cells(r + 1, nameCol) <> "" Then
           'assign the next name
           nextName = Trim(Split(emailWS.Cells(r + 1, nameCol), ",")(1))
        Else
           'this is your EOF exit so assume a name
           nextName = "Exit"
        End If 'Else do noting on this If

        If strName <> previousName Then
            'Set our "new" name to previousName for looping
            'process the "new" name
            previousName = strName
            'create the email object
            Set OutMail = OutApp.CreateItem(0)
            'Process as new email
            With OutMail
                .To = strName 'address email to the name
                .Subject = "Please Approve Divisions" 'appropriate subject
                deptList = strDept & "<br>" 'add the dept to dept list
                'Build the first line of email body in HTML format
                strBody = "<Font Face=calibri>Dear " & strName & ", <br><br> Please approve the following divisions:<br><br>"
            End With
        Else
            'The name is the same as the email we opened
            'Process Dept only by adding it to string with a line break
            deptList = deptList & strDept & "<br>"
        End If

        'Do we send the email and get ready for another?
        If strName <> nextName Then
            'the next name is not the same as the one we are processing and we sorted first
            'so it is time to send the email
            OutMail.HTMLBody = strBody & "<B>" & deptList & "</B>" & "<br><br>" & strSig
            OutMail.Display

        Else 'Do Nohing
        End If

Next r 'move to the next row

'nullify email reference
Set OutMail = Nothing
Set OutApp = Nothing


End Sub

If you want to guard against duplicate departments then I would do it like this, you can see where this goes there is only one end with:

    End With
Else
    'The name is the same as the email we opened
    'Process Dept only by adding it to string with a line break
    If InStr(deptList, strDept) = 0 Then
        'Dept is not in the list so Add the department
        deptList = deptList & strDept & "<br>"
    Else
        'Do nothing, the dept is already there
    End If
End If

I suppose never give up. Everything is possible, maybe just outside of our current skillset (so get some help and keep learning).

Cheers - WWC

Upvotes: 1

Wookies-Will-Code
Wookies-Will-Code

Reputation: 735

Here is a little helper stub I use to find a unique list from column A and place that list in column C. Based on a button click. Modify as you wish.

Option Explicit

Private Sub CommandButton1_Click()
 Dim thisWS As Worksheet
 Dim firstRow As Double
 Dim lastRow As Double
 Dim workCol As Double
 Dim dataRange As Range
 Dim uniqueLast As Double
 Dim uniqueCol As Double
 Dim i As Double
 Dim y As Double
 Dim Temp As String
 Dim found_Bool As Boolean

 Set thisWS = ThisWorkbook.Worksheets("Sheet2")
 workCol = thisWS.Range("A1").Column
 firstRow = 1
 uniqueLast = 1
 uniqueCol = thisWS.Range("C1").Column
 lastRow = thisWS.Cells(thisWS.Rows.Count, workCol).End(xlUp).Row

 For i = firstRow To lastRow
    Temp = Trim(UCase(thisWS.Range(Cells(i, workCol), Cells(i, workCol))))
    Temp = Replace(Temp, "#", "")
    found_Bool = False
    For y = 1 To uniqueLast
        If Temp = thisWS.Range(Cells(y, uniqueCol), Cells(y, uniqueCol)) Then
          found_Bool = True
        Else ' Do nothing
        End If


    Next y

    If found_Bool = False Then
          thisWS.Range(Cells(uniqueLast + 1, uniqueCol), Cells(uniqueLast + 1, uniqueCol)) = Temp
          uniqueLast = uniqueLast + 1
    Else
    End If

 Next i
End Sub

Once you do this you can lookup each name in the non unique column and get the appropriate dept for subject or other info.

What you want is really a pivot in VBA (name & dept(s), you could just vba the pivot, that is a little trickier but very doable.

'***************************************************

OK take what Scott has and its very workable. With regard to the pivot table itself a few "helpers". Again, either name the table and just update the range or delete it and make it each time. Do to the project I delete it every time here and keep using the same space to make picot after picot, every time the workbook is opened this scratch space is clear.

This is me creating a pivot of sales data, bear with me, I actually copy the pivot data afterwards to values and then add columns to perform calculations, then I move that to a report, deleting the pivot and working table, basically this all happens away from what the user gets to see when they click a button:

   '***************************
    'Add Sales Pivot Table
   'Last DR is the last data row, you can see it done several times, in the code below, once you do it you will always do it
   'CalcSheet is the name of the worksheet in the workbook I am working on
   'The range here is defined in Range Format, you could use a named range or use .Range(Cells(row,col),Cells(row,col)) there are several ways
   'I name the pivot table upon creation so I can manipulate it better
   'I specify the target cell, upper left with which to begin the pivot table

   ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        CalcSheet.Range("K14:AY" & LastDR), Version:=xlPivotTableVersion15).CreatePivotTable _
        TableDestination:=CalcSheet.Range("CA37"), TableName:="SalesPVT", DefaultVersion _
        :=xlPivotTableVersion15

I set the pivot up in the format that I want and then I sort it based on one of the fields:

With CalcSheet.PivotTables("SalesPVT").PivotFields("Salesperson")
    .Orientation = xlRowField
    .Position = 1
End With

With CalcSheet.PivotTables("SalesPVT").PivotFields("Customer")
    .Orientation = xlRowField
    .Position = 2
End With

With CalcSheet.PivotTables("SalesPVT").PivotFields("DD Rev")
    .Orientation = xlDataField
    .Function = xlSum
    .NumberFormat = "$#,##0"
End With

With CalcSheet.PivotTables("SalesPVT").PivotFields("Job Days")
    .Orientation = xlDataField
    .Function = xlSum
    .NumberFormat = "#,##0"
End With

CalcSheet.PivotTables("SalesPVT").PivotFields("Salesperson").AutoSort _
    xlDescending, "Sum of DD Rev"

Perhaps there is another way but now I do not know the dimensions of the pivot table (rows) do I? So I define them here based on the first column where I placed the pivot table and the anchor range I specified in creation:

'Find the last row of Pivot table Data

Dim LastPVTrow As Double
Dim FirstPVTrow As Double
Dim NumPVTrows As Double
Dim PivCol As Double

PivCol = CalcSheet.Range("CB37").Column

FirstPVTrow = CalcSheet.Range("CB37").Row
LastPVTrow = CalcSheet.Cells(Rows.Count, PivCol).End(xlUp).Row
NumPVTrows = LastPVTrow - FirstPVTrow

Here I make a column somewhere else based on the pivot data, your email could occur about right here if you wanted: 'make the Avg Rev/Job Day Column

    For i = 1 To NumPVTrows ' four columns in this table
        CalcSheet.Range("CD" & (100 + i)).NumberFormat = "$#,##0"
        If CalcSheet.Range("CC" & (FirstPVTrow + i)) <> 0 Then
            CalcSheet.Range("CD" & (100 + i)) = CalcSheet.Range("CB" & (FirstPVTrow + i)) / CalcSheet.Range("CC" & (FirstPVTrow + i))
        Else
            CalcSheet.Range("CD" & (100 + i)) = 0
        End If
   Next i

'Here I am going to leave a bunch of stuff out but it puts headers on my table that is only missing the pivot and adds some more columns and calculations, counts the values based on specified ranges etc and finds averages

'Then I copy the pivot table and delete it, happens every time a button is clicked and a new workbook is selected to process

'copy pivot table to get rid of it
 CalcSheet.PivotTables("SalesPVT").TableRange1.Copy
'Paste it as values with formatting
CalcSheet.Range("CA100").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

'Delete Sales Pivot from the file
 CalcSheet.PivotTables("SalesPVT").TableRange1.Delete

'Clear Work Space
CalcSheet.Range("CA1:CN500").Clear

Once I have processed the sales persons, I do it again by customer in the same working scratch space, build a table make new columns and headers down below based on the data, copy the table as values and then after putting it into a report, delete it all, for the next go around. I format my little table before export: bolding the headers, putting grey on the sales person or the customer, the totals line is blue, I right align the numbers in the cell, there sis alot of code left out to focus on the pivot.

So here is similar pivot code building the table for customers

'***************************************
'Make the Customer Pivot and table
'***************************************

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        CalcSheet.Range("K14:AY" & LastDR), Version:=xlPivotTableVersion15).CreatePivotTable _
        TableDestination:=CalcSheet.Range("CA37"), TableName:="CustPVT", DefaultVersion _
        :=xlPivotTableVersion15

    With CalcSheet.PivotTables("CustPVT").PivotFields("Customer")
        .Orientation = xlRowField
        .Position = 1
    End With

    With CalcSheet.PivotTables("CustPVT").PivotFields("DD Rev")
        .Orientation = xlDataField
        .Function = xlSum
        .NumberFormat = "$#,##0"
    End With

    With CalcSheet.PivotTables("CustPVT").PivotFields("Job Days")
        .Orientation = xlDataField
        .Function = xlSum
        .NumberFormat = "#,##0"
    End With

'Find the last row of Pivot table Data

FirstPVTrow = CalcSheet.Range("CA37").Row
LastPVTrow = CalcSheet.Cells(Rows.Count, PivCol).End(xlUp).Row
'LastPVTrow = CalcSheet.Range("CB37:CB500").Find((0), LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
NumPVTrows = LastPVTrow - FirstPVTrow

etc. etc. etc . . .

I am sure the users on here are a lot more elegant. I strive for code that is readable and usually understandable by me (hopefully others) and limited by my skillset, you have to come back to this stuff months or years later, trust me it looks different than when you are "living in the moment of creation" Take the time to leave yourself bread crumbs, name your variables and your tables so they make sense. Try an use named ranges rather than "hard coding" ranges, I know I did it here, do as I say . . . not as I do. I will usually only do this in areas that will later be erased and wiped. No excuses but I was moving in a rush on this one.

Cheers

Upvotes: 1

ASH
ASH

Reputation: 20342

I think you can use this to do what you want to do.

Make a list in Sheets("Sheet1") with :

In column A : Names of the people
In column B : E-mail addresses
In column C:Z : Filenames like this C:\Data\Book2.xls (don't have to be Excel files)

The Macro will loop through each row in "Sheet1" and if there is a E-mail address in column B and file name(s) in column C:Z it will create a mail with this information and send it.

Sub Send_Files()
'Working in Excel 2000-2016
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
    Dim OutApp As Object
    Dim OutMail As Object
    Dim sh As Worksheet
    Dim cell As Range
    Dim FileCell As Range
    Dim rng As Range

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set sh = Sheets("Sheet1")

    Set OutApp = CreateObject("Outlook.Application")

    For Each cell In sh.Columns("B").Cells.SpecialCells(xlCellTypeConstants)

        'Enter the path/file names in the C:Z column in each row
        Set rng = sh.Cells(cell.Row, 1).Range("C1:Z1")

        If cell.Value Like "?*@?*.?*" And _
           Application.WorksheetFunction.CountA(rng) > 0 Then
            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .to = cell.Value
                .Subject = "Testfile"
                .Body = "Hi " & cell.Offset(0, -1).Value

                For Each FileCell In rng.SpecialCells(xlCellTypeConstants)
                    If Trim(FileCell) <> "" Then
                        If Dir(FileCell.Value) <> "" Then
                            .Attachments.Add FileCell.Value
                        End If
                    End If
                Next FileCell

                .Send  'Or use .Display
            End With

            Set OutMail = Nothing
        End If
    Next cell

    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
End Sub

Upvotes: 0

Stefan Bauer
Stefan Bauer

Reputation: 41

I'm using a different technic to solve the same problem with Excel. First of all I have a Function to open a new ADODB-Recordset:

Function RST_Excel(strExceldatei As String, strArbeitsblatt As String, strWHERE As String, Optional strBereich As String, _
                   Optional strDatenfelder As String = "*") As ADODB.Recordset

Dim i As Integer
Dim rst As ADODB.Recordset
Dim strConnection As String
Dim strSQL As String


On Error GoTo sprFehler

strConnection = "DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & strExceldatei

If global_con Is Nothing Then

   Set global_con = New ADODB.Connection

   With global_con
        .Open strConnection
        End With

   End If

strSQL = "SELECT " & strDatenfelder & " FROM [" & strArbeitsblatt & "$" & strBereich & "] WHERE " & strWHERE

Set rst = New ADODB.Recordset

With rst
     .Source = strSQL
     .CursorLocation = adUseClient
     .ActiveConnection = global_con
     .Open
     Set RST_Excel = rst
     End With

sprEnde:

Set rst = Nothing

Exit Function


sprFehler:
Set rst = Nothing

Set RST_Excel = Nothing

End Function

Then I open the ADODB-Recordset from another Routine:

Dim strWHERE As String
Dim strFields As String
Dim rst_Recipients As ADODB.Recordset

strWHERE = "Surname IS NOT NULL AND Emailadress IS NOT NULL"
strFields = "Surname, Name, Emailadress, SMIME"

Set rst_Empfänger = RST_Excel(ThisWorkbook.FullName, "Email", strWHERE, "A1:M1000", strFields)

As the query is passed as an SQL-Statement you could also pass an Statement to get unique results.

The advance for me is that I could easily move through the Recordset:

With rst
     .movefirst

     do until .eof
        debug.print .fields("surename").value
        .movenext
        loop

end with

Upvotes: 0

Scott Holtzman
Scott Holtzman

Reputation: 27259

If you pivot your data, here is a way to loop through the pivot to get unique information by name.

Pivotted Data

enter image description here

Code

Option Explicit

Sub LoopPivot()

With Sheet1

    Dim pt As PivotTable
    Set pt = .PivotTables(1)

    Dim nameField As PivotField
    Set nameField = pt.PivotFields("Name")

    Dim nameItem As PivotItem

    For Each nameItem In nameField.PivotItems

        Dim name As String
        name = nameItem.Value

        Dim emailField As PivotField
        Set emailField = pt.PivotFields("email")

        Dim emailItem As PivotItem
        Set emailItem = emailField.PivotItems(nameItem.Position)

        Dim email As String
        email = emailItem.Value

        Dim divisionName As Range

        Dim division As String
        division = vbNullString

        For Each divisionName In nameItem.DataRange

            division = division & "," & divisionName.Value

        Next

        division = Mid(division, 2, 255)

        Debug.Print name
        Debug.Print email
        Debug.Print division


    Next

End With


End Sub

Upvotes: 1

Related Questions