Supposing you want to send an email through Outlook to a certain recipient based on a specified cell value in Excel. For example, when the value of cell D7 in a worksheet is greater than 200, then an email is created automatically. This article introduces a VBA method for you to quickly solve this issue.
Please do as follows to send an email based on cell value in Excel.
1. In the worksheet you need to send email based on its cell value (here says the cell D7), right-click the sheet tab, and select View Code from the context menu. See screenshot:
2. In the popping up Microsoft Visual Basic for Applications window, please copy and paste the below VBA code into the sheet code window.
VBA code: Send email through Outlook based on cell value in Excel
Dim xRg As Range 'Update by Extendoffice 2018/3/7 Private Sub Worksheet_Change(ByVal Target As Range) On Error Resume Next If Target.Cells.Count > 1 Then Exit Sub Set xRg = Intersect(Range("D7"), Target) If xRg Is Nothing Then Exit Sub If IsNumeric(Target.Value) And Target.Value > 200 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 = "Hi there" & vbNewLine & vbNewLine & _ "This is line 1" & vbNewLine & _ "This is line 2" On Error Resume Next With xOutMail .To = "Email Address" .CC = "" .BCC = "" .Subject = "send by cell value test" .Body = xMailBody .Display 'or use .Send End With On Error GoTo 0 Set xOutMail = Nothing Set xOutApp = Nothing End Sub
1. In the VBA code, D7 and value > 200 are the cell and cell value you will send email based on.
2. Please change the email body as you need in xMailBody line in the code.
3. Replace the Email Address with the recipient email address in line .To = "Email Address".
4. And specify the Cc and Bcc recipients as you need in .CC = “” and Bcc = “” sections.
5. Finally change the email subject in line .Subject = "send by cell value test".
3. Press the Alt + Q keys together to close the Microsoft Visual Basic for Applications window.
From now on, when the value you entering in cell D7 is greater than 200, an email with specified recipients and body will be created automatically in Outlook. You can click the Send button to send this email. See screenshot:
1. The VBA code is only working when you use Outlook as your email program.
2. If the entered data in cell D7 is a text value, the email window will be popped out as well.
Easily send email through Outlook based on fields of created mailing list in Excel:
The Send Emails utility of Kutools for Excel helps users sending email through Outlook based on created mailing list in Excel.
Download and try it now! ( 30-day free trail)