Creating a dynamic Subject line from information in a particular Excel column

StevieMP

Board Regular
Joined
Sep 28, 2021
Messages
73
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi There All,

I have the following code which works quite well which I would like to update to incorporate a more dynamic "Subject" within an Email.
I have a spreadsheet with 2 sheets - In sheet 1, there is a cell with the Subject for an Email Subject Line along with a table which gets imported into an email.

Sheet 1 Column AColumn BColumn C
File Location :Z:\GMM\
Subject :MM - Test for FIN
LENameTest1
FNameTest2
DCodeTest3
CBankTest4
NAValueTest5

In sheet 2, there is another table with names in column A, Yes/No in column B, email addresses in column C and file addresses in column E.

Sheet 2
Column AColumn BColumn CColumn DColumn E
BA NAYestestemail;(BA)Z:\GMM\Test.pdf
BNYMYestestemail(BNYM)Z:\GMM\Test1.pdf

What I would like the code to do is to add the information from Sheet 2 in column D to the Subject in column C, if the information in column B is Yes. If no, move on to the next line.
So the Subject line in each email will read:
First Email
Subject : MM - Test for FIN (BA)

Second Email
Subject : MM - Test for FIN (BNYM)

The code I have is :

Sub MMT()

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


dpath = Sheet1.Range("C7")


'Look in column B9 onward, where Yes/No would reside
LastRow = Sheet8.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, Mail Body 1 row below
emailTo = Cells(i, 2 + 1).Value
' emailBody = Sheet3.Range("B10 - C22").Value



'looping through all the files and attaching them to an Email

iRow = 9


' We'll now set a range for the .Subject entered into the "Money Market" tab in Cell "C8".
Set myDataRng3 = Sheet3.Range("C8:C8") '& Cells(Rows.Count, "C8").End(xlUp).Row)


' Run a loop to extract email ids from the 3rd column.
For Each cell In myDataRng3
If Trim(subj) = "" Then
subj = cell.Offset(0, 0).Value
Else
subj = subj & vbCrLf & ";" & cell.Offset(0, 0).Value
End If
Next cell

Set myDataRng = Nothing ' Clear the range.

'=========================================================================================
'First Range called MMRange can be increased if multiples are required and these will appear in the email.

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("MMRange").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


'==========================================================================================
'Second Range called B24:C50 is the wording for the email.

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

If rng2 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:Arial>" & _
"Good Afternoon, <br><br> Please open under this arrangement. <br>"

str2 = "<br>If you have any queries with regard to this matter, then please contact me.<br>"

str3 = "<br>Best regards,<br> Stephen"

'Dim OutApp As Object
'Dim OutMail As Object

On Error Resume Next
With OutMail
.To = emailTo ' Assign all email ids to the property.
.CC = "Stephen.Preece" ' Assign all email ids to the property.
.BCC = ""
.Subject = subj
.HTMLBody = str1 & RangetoHTML(rng) & RangetoHTML(rng2) & .HTMLBody & str2 & str3 '.HTMLBody


'Selects all the files listed in column E

Do While Cells(iRow, 5) <> Empty

'picking up file name from column E (5)
pfile = Dir(dpath & "\" & Cells(iRow, 5) & "" & "")

'checking for file exist in a folder and if its a requested file from the list
'.Attachments.Add (dpath & Cells(irow, 5) & pfile)
.Attachments.Add (Cells(iRow, 5) & pfile)

'go to next file listed in column E (5)
iRow = iRow + 1
Loop


.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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,223,885
Messages
6,175,186
Members
452,615
Latest member
bogeys2birdies

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