Forum Discussion

DanielaBarros88's avatar
DanielaBarros88
Copper Contributor
Nov 23, 2023

Email to be sent when a content is entered in a cell

Hello guys,

 

I would like to receive an automated email when a person enters a data in a specific cell of a spreadsheet. Is that possible? 

 

Thank you 🙂

  • DanielaBarros88 

    it is possible to set up an automated email notification when a specific cell in an Excel spreadsheet is updated. You can achieve this using a combination of Excel's Worksheet Change event and Outlook VBA (Visual Basic for Applications) code. Here's a step-by-step guide:

    Enable Developer Tab:

    1. Open Excel and go to the "File" tab.
    2. Click on "Options."
    3. In the Excel Options dialog box, select "Customize Ribbon."
    4. Check the "Developer" option and click "OK."

    Open the VBA Editor:

    1. Click on the "Developer" tab in the Ribbon.
    2. Click on "Visual Basic" to open the VBA Editor.

    Write VBA Code:

    Paste the following VBA code into the code window for the sheet where you want to monitor changes. Replace the email address and other details with your actual information.

    vba code (is untested):

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim WatchRange As Range
        Dim IntersectRange As Range
        Dim OutlookApp As Object
        Dim OutlookMail As Object
        
        ' Define the range to watch for changes
        Set WatchRange = Range("A1") ' Change A1 to the cell you want to monitor
        
        ' Check if a change occurred in the watch range
        If Not Intersect(Target, WatchRange) Is Nothing Then
            ' Create Outlook objects
            Set OutlookApp = CreateObject("Outlook.Application")
            Set OutlookMail = OutlookApp.CreateItem(0)
            
            ' Build the email
            With OutlookMail
                .To = "email address removed for privacy reasons" ' Replace with the recipient's email address
                .Subject = "Cell Value Updated"
                .Body = "The value in cell A1 has been updated to: " & WatchRange.Value
                ' You can customize the email body as needed
                .Send
            End With
            
            ' Release Outlook objects
            Set OutlookMail = Nothing
            Set OutlookApp = Nothing
        End If
    End Sub

    Test the Code:

    1. Change the recipient's email address and customize the email body as needed.
    2. Close the VBA Editor.
    3. Enter data into the specified cell (e.g., A1) to trigger the email.

    Important Note:

    • Keep in mind that VBA code will only run if macros are enabled in your Excel settings. Some organizations may have security policies that restrict the execution of macros.
    • The email will be sent from the default Outlook profile on the computer where the code is running.
    • This solution assumes you have Outlook installed and configured on your computer.

    Please be aware of the potential security implications when using VBA code, especially if the spreadsheet is shared or distributed. The text was created with the help of AI.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

     

    Was the answer useful? Mark as best response and like it!

    This will help all forum participants.

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    DanielaBarros88 

    it is possible to set up an automated email notification when a specific cell in an Excel spreadsheet is updated. You can achieve this using a combination of Excel's Worksheet Change event and Outlook VBA (Visual Basic for Applications) code. Here's a step-by-step guide:

    Enable Developer Tab:

    1. Open Excel and go to the "File" tab.
    2. Click on "Options."
    3. In the Excel Options dialog box, select "Customize Ribbon."
    4. Check the "Developer" option and click "OK."

    Open the VBA Editor:

    1. Click on the "Developer" tab in the Ribbon.
    2. Click on "Visual Basic" to open the VBA Editor.

    Write VBA Code:

    Paste the following VBA code into the code window for the sheet where you want to monitor changes. Replace the email address and other details with your actual information.

    vba code (is untested):

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim WatchRange As Range
        Dim IntersectRange As Range
        Dim OutlookApp As Object
        Dim OutlookMail As Object
        
        ' Define the range to watch for changes
        Set WatchRange = Range("A1") ' Change A1 to the cell you want to monitor
        
        ' Check if a change occurred in the watch range
        If Not Intersect(Target, WatchRange) Is Nothing Then
            ' Create Outlook objects
            Set OutlookApp = CreateObject("Outlook.Application")
            Set OutlookMail = OutlookApp.CreateItem(0)
            
            ' Build the email
            With OutlookMail
                .To = "email address removed for privacy reasons" ' Replace with the recipient's email address
                .Subject = "Cell Value Updated"
                .Body = "The value in cell A1 has been updated to: " & WatchRange.Value
                ' You can customize the email body as needed
                .Send
            End With
            
            ' Release Outlook objects
            Set OutlookMail = Nothing
            Set OutlookApp = Nothing
        End If
    End Sub

    Test the Code:

    1. Change the recipient's email address and customize the email body as needed.
    2. Close the VBA Editor.
    3. Enter data into the specified cell (e.g., A1) to trigger the email.

    Important Note:

    • Keep in mind that VBA code will only run if macros are enabled in your Excel settings. Some organizations may have security policies that restrict the execution of macros.
    • The email will be sent from the default Outlook profile on the computer where the code is running.
    • This solution assumes you have Outlook installed and configured on your computer.

    Please be aware of the potential security implications when using VBA code, especially if the spreadsheet is shared or distributed. The text was created with the help of AI.

     

    My answers are voluntary and without guarantee!

     

    Hope this will help you.

     

    Was the answer useful? Mark as best response and like it!

    This will help all forum participants.

    • omarjamal's avatar
      omarjamal
      Copper Contributor

      NikolinoDE 

       

      Please I want the (WatchRange) a range of column "A1:A20" instead of one cell "A1" ?
      Thank you

      • NikolinoDE's avatar
        NikolinoDE
        Gold Contributor

        omarjamal 

        To monitor a range of cells instead of just one, you can modify the WatchRange to cover the desired range (e.g., A1:A20).

         

        VBA Code is untested backup your file first.

        Private Sub Worksheet_Change(ByVal Target As Range)
            Dim WatchRange As Range
            Dim IntersectRange As Range
            Dim OutlookApp As Object
            Dim OutlookMail As Object
            
            ' Define the range to watch for changes
            Set WatchRange = Range("A1:A20") ' Monitor the range A1:A20
            
            ' Check if the change occurred in the watch range
            Set IntersectRange = Intersect(Target, WatchRange)
            If Not IntersectRange Is Nothing Then
                ' Create Outlook objects
                Set OutlookApp = CreateObject("Outlook.Application")
                Set OutlookMail = OutlookApp.CreateItem(0)
                
                ' Build the email
                With OutlookMail
                    .To = "email address removed for privacy reasons" ' Replace with the recipient's email address
                    .Subject = "Cell Value Updated"
                    .Body = "The value in cell " & Target.Address & " has been updated to: " & Target.Value
                    ' You can customize the email body as needed
                    .Send
                End With
                
                ' Release Outlook objects
                Set OutlookMail = Nothing
                Set OutlookApp = Nothing
            End If
        End Sub

         

        Make sure to enable macros.

        Change the recipient's email address.

        Enter or update data in any cell within the A1

        range to trigger the email.

        This will trigger the email notification when a change is made in any cell within the range "A1.

         

        My answers are voluntary and without guarantee!

         

        Hope this will help you.