Hi, apologies Im a little new to this but I have a spreadsheet that is intended to automatically send an email based on the result of a formula. The code I have in ThisWorksheet for sending the email is:
And in the sheet monitoring the value I have put this code:
Any help appreciated how I can get this to work automatically. Thanks
Code:
Sub Test1()
Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range
Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")
On Error GoTo cleanup
For Each cell In Columns("AH").Cells.SpecialCells(xlCellTypeConstants)
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "AG").Value) = "yes" _
And LCase(Cells(cell.Row, "AJ").Value) <> "yes" Then
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = cell.Value
.Subject = "Reminder"
.Body = "Dear " & Cells(cell.Row, "J").Value _
& vbNewLine & vbNewLine & _
"Action. " & Cells(cell.Row, "AF").Value
.Send
End With
On Error GoTo 0
Cells(cell.Row, "AJ").Value = "yes"
Cells(cell.Row, "AK").Value = Date
Set OutMail = Nothing
End If
Next cell
cleanup:
Set OutApp = Nothing
Application.ScreenUpdating = True
End Sub
Private Sub Worksheet_Calculate()
Worksheet_Change Range("AG:AG")
End Sub
And in the sheet monitoring the value I have put this code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("AG1:AG100")) Is Nothing Then
Call ThisWorkbook.Test1
End If
End Sub
Last edited by a moderator: