How to send email if a certain cell is modified in Excel?
This article is talking about sending an email through Outlook when a cell in a certain range is modified in Excel.
Send email if cell in a certain range is modified with VBA code
Send email if cell in a certain range is modified with VBA code
If you need to create a new email automatically with active workbook attached when a cell in range A2:E11 is modified in a certain worksheet, the following VBA code can help you.
1. In the worksheet which you need to send email based on its modified cell in a certain range, right-click the sheet tab and then click the View Code from the context menu. See screenshot:
2. In the popping up Microsoft Visual Basic for Applications window, please copy and paste below VBA code into the Code window.
VBA code: Send email if cell in a specified range is modified in Excel
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 2017/9/12
Dim xRgSel As Range
Dim xOutApp As Object
Dim xMailItem As Object
Dim xMailBody As String
On Error Resume Next
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set xRg = Range("A2:E11")
Set xRgSel = Intersect(Target, xRg)
ActiveWorkbook.Save
If Not xRgSel Is Nothing Then
Set xOutApp = CreateObject("Outlook.Application")
Set xMailItem = xOutApp.CreateItem(0)
xMailBody = "Cell(s) " & xRgSel.Address(False, False) & _
" in the worksheet '" & Me.Name & "' were modified on " & _
Format$(Now, "mm/dd/yyyy") & " at " & Format$(Now, "hh:mm:ss") & _
" by " & Environ$("username") & "."
With xMailItem
.To = "Email Address"
.Subject = "Worksheet modified in " & ThisWorkbook.FullName
.Body = xMailBody
.Attachments.Add (ThisWorkbook.FullName)
.Display
End With
Set xRgSel = Nothing
Set xOutApp = Nothing
Set xMailItem = Nothing
End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Notes:
3. Press the Alt + Q keys simultaneously to close the Microsoft Visual Basic for Applications window.
From now on, any cell in range A2:E11 is modified, a new email will be created with the updated workbook attached. And all specified fields such as subject, recipient and email body will be listed in the email. Please send the email.
Note: The VBA code works only if you are using Outlook as your email program.
Related articles:
- How to automatically send email based on cell value in Excel?
- How to send an email through Outlook when workbook is saved in Excel?
- How to send email if button is clicked in Excel?
- How to send email if due date has been met in Excel?
- How to send email reminder or notification if workbook is updated in Excel?
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!