Email Alert Excel Using VBA

daddyfoxuk

Board Regular
Joined
Nov 18, 2016
Messages
68
Hi All, i'm wanting to create an auto email to be sent from excel when a cell in I equals more than one, kind of like an alert... However i would like this email to include certain cells of information depending on which cell in I has changed. For example if I10 changes i would like cells A10 to H10 to be copied and sent in the email.

I have the below code but i'm at a loss right now.... Any help or suggestion would be much appreciated! Thank You!!

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("I1:I10000"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value > 1 Then
Call Mail_small_Text_Outlook
End If
End Sub
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Suspect Part Found" & vbNewLine & vbNewLine & _
"Containment Inspection" & vbNewLine & _
"Please Contact Inspection Area"
On Error Resume Next
With xOutMail
.To = "My Email"
.CC = ""
.BCC = ""
.Subject = "Suspect Part Located"
.Body = xMailBody
.Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
How about this...


Code:
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Dim cel As Range
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Suspect Part Found" & vbNewLine & vbNewLine & _
"Containment Inspection" & vbNewLine & _
"Please Contact Inspection Area" & vbNewLine


For Each cel In Range("A" & Target.Row & ":H" & Target.Row)
    xMailBody = xlmailbody & cel.Value & vbLf
Next cel


On Error Resume Next
With xOutMail
.To = "My Email"
.CC = ""
.BCC = ""
.Subject = "Suspect Part Located"
.Body = xMailBody
.Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 
Upvote 0
Hi, thanks.... Im now getting a runtime error and the emails arent sending :(


How about this...


Code:
Sub Mail_small_Text_Outlook()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Dim cel As Range
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Suspect Part Found" & vbNewLine & vbNewLine & _
"Containment Inspection" & vbNewLine & _
"Please Contact Inspection Area" & vbNewLine


For Each cel In Range("A" & Target.Row & ":H" & Target.Row)
    xMailBody = xlmailbody & cel.Value & vbLf
Next cel


On Error Resume Next
With xOutMail
.To = "My Email"
.CC = ""
.BCC = ""
.Subject = "Suspect Part Located"
.Body = xMailBody
.Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 
Upvote 0
I see what I did wrong. Both subs need to be changed.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("I1:I10000"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value > 1 Then
Call Mail_small_Text_Outlook(Target.Row)
End If
End Sub

Code:
Sub Mail_small_Text_Outlook(tRow)
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Dim cel As Range
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "Suspect Part Found" & vbNewLine & vbNewLine & _
"Containment Inspection" & vbNewLine & _
"Please Contact Inspection Area" & vbNewLine


For Each cel In Range("A" & tRow & ":H" & tRow)
    xMailBody = xMailBody & cel.Value & vbLf
Next cel


On Error Resume Next
With xOutMail
.To = "My Email"
.CC = ""
.BCC = ""
.Subject = "Suspect Part Located"
.Body = xMailBody
.display
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 
Upvote 0
HI, thanks but all im getting now is a box asking me to create a macro when i try and run the code...?
 
Upvote 0
The code needs to be triggered by the worksheet event. The Mail_small_Text_Outlook sub is receiving a variable, so you can't run it from the editor.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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