VBA - Group by value in table and send e-mail

oliverbusk

New Member
Joined
Jan 11, 2018
Messages
1
I have a large list where I have different companies and organisations. My current VBA codes iterates through each table row, and send out an e-mail for each line.

However, what I need to achieve is that there might be companies which have the same parent organisation - in this case, I don't want to send out an e-mail for each company, but would rather send out one e-mail to the organisation, including all the associated companies.

I have tried to illustrate it in below table.



So for record #1 , I would just send out an e-mail as always. However for [2-3], I would like to send out one e-mail, which includes company name 2 and 3. Same goes for record [4-6], which should also send out one e-mail, but include company name #4 , #5 and #6 .

Example:

Hi Organisation B

Please check below companies in your organisation:

  • Company 2
  • Company 3


My current code: Below is my current code, which iterates through all table rows and send out mails:
Code:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">'Count table rows
 Set tbl = oWorksheet.ListObjects("DataTable")
 TableRows = tbl.ListRows.Count
 TableRows = TableRows + 1 'Skips the header
 iRow = 2 'Skip the header

 Do While iRow <= TableRows
       Set oNotesMail = oMailDB.CreateDocument
       oNotesMail.Form = "Memo"
       oNotesMail.SendTo = oWorksheet.Cells(iRow, 5).Value
       oNotesMail.Subject = "Company check"
       oNotesMail.Body = "Hi " & oWorksheet.Cells(iRow, 2) & "Please check"
       oNotesMail.SaveMessageOnSend = True
       oNotesMail.PostedDate = Now()
       oNotesMail.Send 0, oWorksheet.Cells(iRow, 5).Value
       Set oNotesMail = Nothing
    Loop
</code>

So above code send out an email (in Lotus Notes) for each line.
How can I achieve my example above?

Please note

  • The table will always be sorted by 'Organisation' - hopefully this will help when the code is iterating through the rows.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,215,009
Messages
6,122,674
Members
449,091
Latest member
peppernaut

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