VBA code to create an Email based on information in columns and take data from another sheet in the same spreadsheet

StevieMP

New Member
Joined
Sep 28, 2021
Messages
43
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good Morning,

I have a spreadsheet consisting of 2 tabs.
First tab is called "MM" - this consists of a table, 2 columns by 13 rows with several rows below consisting of a few sentences. The table could increase to 4 or 6 columns but will always be 13 rows.

Legal NameStephen Preece
Other NameTEST
CodeTEST
BankTEST
ValueTEST
DomicileTEST
TypeTEST
DateTEST
InstructionsTEST
DetailsTEST
AddressTEST
AdminTEST
ListTEST

Second tab is called "Contacts MM" - this consists of 3 columns A, B & C where:
Column A shows names of individuals
Column B shows either "Yes" or "No"
Column C shows email address which may have one email or several emails on the same line semi colon delimited.

A B C
Joe BloggsNoemail address;
Michael SmithYesemail address;
Stephen PreeceYesemail address; email address;

What I would like to do is have some VBA code to go through column B and all the rows that have a "Yes" take the email addresses in column C and create just the one email and add all the email addresses to .BCC

Then take the information in the First tab "MM" and add it to the body of the email

Can you help please?

Thank you in advance
Stephen
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Good Morning,

I have a spreadsheet consisting of 2 tabs.
First tab is called "MM" - this consists of a table, 2 columns by 13 rows with several rows below consisting of a few sentences. The table could increase to 4 or 6 columns but will always be 13 rows.

Legal NameStephen Preece
Other NameTEST
CodeTEST
BankTEST
ValueTEST
DomicileTEST
TypeTEST
DateTEST
InstructionsTEST
DetailsTEST
AddressTEST
AdminTEST
ListTEST

Second tab is called "Contacts MM" - this consists of 3 columns A, B & C where:
Column A shows names of individuals
Column B shows either "Yes" or "No"
Column C shows email address which may have one email or several emails on the same line semi colon delimited.

A B C
Joe BloggsNoemail address;
Michael SmithYesemail address;
Stephen PreeceYesemail address; email address;

What I would like to do is have some VBA code to go through column B and all the rows that have a "Yes" take the email addresses in column C and create just the one email and add all the email addresses to .BCC

Then take the information in the First tab "MM" and add it to the body of the email

Can you help please?

Thank you in advance
Stephen

Morning Anyone,

I forgot to post the code I have already which works but loads a new email every time column B has a "Yes". I only want one email created with all the email addresses relating to column B, so in the example above it would be .BCC email address; email address; email address;

Can someone help?

Sub MM()

Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Dim str1, str2, str3 As String
Dim subj As String
Dim Worksheet As Range
Dim ThisWorkbook As Object
Dim sMail_ids As String
Dim sMail_ids2 As String


'Look in column B2 onward, where Yes/No would reside
LastRow = Range("B" & Rows.Count).End(xlUp).Row

For i = 2 To LastRow

If UCase(Cells(i, 2).Value) = "YES" Then

'Address is offset 1 row above
emailTo = Cells(i, 2 + 1).Value


Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a fixed range if you want
Set rng = Sheets("Money Market").Range("B10:C22").SpecialCells(xlCellTypeVisible)
On Error GoTo 0

If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If

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

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

str1 = "<Body style = font-size:11pt;font-family:Calibri>" & _
"<br>TEST"

str2 = "<br>TEST"

str3 = "<br>TEST"

'Dim OutApp As Object
'Dim OutMail As Object

On Error Resume Next
With OutMail
.To = ""
.CC = ""
.BCC = emailTo
.Subject = subj
.HTMLBody = str1 & RangetoHTML(rng) & str2 & .HTMLBody

.Display
End With

Set OutMail = Nothing
Set OutApp = Nothing
End If

'Cells(i, 2).value = "No"
Next

End Sub


Function RangetoHTML(rng As Range)
'
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"

'Copy the range and create a new workbook to past the data in
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

'Publish the sheet to a htm file
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

'Read all data from the htm file into RangetoHTML
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=")

'Close TempWB
TempWB.Close SaveChanges:=False

'Delete the htm file we used in this function
Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
 
Upvote 0

Forum statistics

Threads
1,213,495
Messages
6,113,992
Members
448,538
Latest member
alex78

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