Automatically populate cell after vba email sent

eddieum1

New Member
Joined
Feb 10, 2023
Messages
7
Office Version
  1. 2019
Platform
  1. Windows
Hi there,

I'm new to VBA and using Ron's Email template to automatically send messages. However, I'd like to only send one email without having to delete the excel workbook. Instead, I'd like to set up a rule where only emails are sent if column N is blank. In other words, the word "Sent" is automatically populated after the first email is sent. Can anyone help modify the code please?


Sub mail2()
'For Tips see: Excel Automation - Ron de Bruin
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Dim Ash As Worksheet
Dim Cws As Worksheet
Dim Rcount As Long
Dim Rnum As Long
Dim FilterRange As Range
Dim FieldNum As Integer
Dim StrBody As String

StrBody = "</center><p style='font-family:calibri;font-size:15'>Hi there," & "<br><br>" & _
"<p style='font-family:calibri;font-size:15'>Hi there " & "<br>" & _
"" & ""

On Error GoTo cleanup
Set OutApp = CreateObject("Outlook.Application")

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

'Set filter sheet, you can also use Sheets("MySheet")
Set Ash = Sheets("Watchlist")

'Set filter range and filter column (column with e-mail addresses)
Set FilterRange = Ash.Range("A1:N" & Ash.Rows.Count)
FieldNum = 3 'Filter column = C because the filter range start in column A

'Add a worksheet for the unique list and copy the unique list in A1
Set Cws = Worksheets.Add
FilterRange.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=Cws.Range("A1"), _
CriteriaRange:="", Unique:=True

'Count of the unique values + the header cell
Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))

With Worksheets("Watchlist").Cells
.Columns("D").AutoFit
.Columns("F:J").AutoFit
.Columns("L").AutoFit
End With

'If there are unique values start the loop
If Rcount >= 2 Then
For Rnum = 2 To Rcount

'Filter the FilterRange on the FieldNum column
FilterRange.AutoFilter Field:=FieldNum, _
Criteria1:=Cws.Cells(Rnum, 1).Value

'--also filter Status field for no responses
FilterRange.AutoFilter Field:=14, Criteria1:=""

'If the unique value is a mail address create a mail
If Cws.Cells(Rnum, 1).Value Like "?*@?*.?*" Then

With Ash.AutoFilter.Range
On Error Resume Next
With .Resize(.Rows.Count, .Columns.Count - 4)
Set rng = .SpecialCells(xlCellTypeVisible)
End With
On Error GoTo 0
End With

Set OutMail = OutApp.CreateItem(0)


On Error Resume Next
With OutMail
.Display
.to = Cws.Cells(Rnum, 1).Value
.Subject = "Hi there"
.HTMLBody = StrBody & RangetoHTML(rng) & .HTMLBody
.Send 'Or use Send
End With

On Error GoTo 0

Set OutMail = Nothing
End If

'Close AutoFilter
Ash.AutoFilterMode = False

Next Rnum

End If

cleanup:
Set OutApp = Nothing
Application.DisplayAlerts = False
Cws.Delete
Application.DisplayAlerts = True

With Application
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
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 paste the data in
rng.Offset(0, 3).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=")
RangetoHTML = Replace(RangetoHTML, "table border=0 cellpadding=0 cellspacing=0", _
"table border=0 cellpadding=1 cellspacing=1")

'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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
ok, so i was able to use .Range("M" & Rnum).Value = "Sent" but it only populates one time. If i add the same email address and send emails, the cell won't populate for the second time. Any guidance would be appreciated

With Ash.AutoFilter.Range
On Error Resume Next
With .Resize(.Rows.Count, .Columns.Count - 3)
.Range("M" & Rnum).Value = "Sent"
Set rng = .SpecialCells(xlCellTypeVisible)
End With
On Error GoTo 0
End With
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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