Forum Discussion

medovina's avatar
medovina
Copper Contributor
Aug 19, 2024

How to send notification when a live feed Excel cell changes value

I have a live data feed inputting to Excel (365) and would like to get notification via txt or email when specific cells hit target values.  Is it possible to do this with a Visual Basic macro?

  • 1. Set Up the Worksheet Change Event
    Open the VBA Editor:
    Press ALT + F11 to open the VBA editor.
    Locate Your Worksheet:
    In the VBA editor, find the worksheet where your live data feed is located. This will be under VBAProject > Microsoft Excel Objects.
    Add the Worksheet Change Event:
    Double-click on the worksheet (e.g., Sheet1) where you want to monitor changes.
    Add the following code to the Worksheet_Change event:
    vba
    Copy code
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    Dim watchCells As Range

    ' Define the range of cells you want to monitor
    Set watchCells = Me.Range("A1:B10") ' Adjust range as needed

    ' Check if the changed cell is within the monitored range
    If Not Intersect(Target, watchCells) Is Nothing Then
    For Each cell In Target
    ' Check if cell value meets the target condition
    If cell.Value >= 100 Then ' Adjust condition as needed
    Call SendNotification(cell.Address, cell.Value)
    End If
    Next cell
    End If
    End Sub
    2. Create a Subroutine to Send Notifications
    You can use VBA to send an email via Outlook. To send a text message, you’ll need to use an email-to-SMS gateway provided by mobile carriers. Here’s how you can set up email notifications:

    Add the Notification Subroutine:
    In the VBA editor, insert a new module by clicking Insert > Module.
    Add the following code to send an email notification:
    vba
    Copy code
    Sub SendNotification(cellAddress As String, cellValue As Variant)
    Dim OutlookApp As Object
    Dim OutlookMail As Object

    ' Create an instance of Outlook
    Set OutlookApp = CreateObject("Outlook.Application")
    Set OutlookMail = OutlookApp.CreateItem(0)

    ' Set up the email
    With OutlookMail
    .To = "email address removed for privacy reasons" ' Replace with recipient's email address
    .Subject = "Cell Value Alert"
    .Body = "The value in cell " & cellAddress & " has changed to " & cellValue & "."
    .Send
    End With

    ' Clean up
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing
    End Sub
    • medovina's avatar
      medovina
      Copper Contributor

      AshaKantaSharma 

       

      Thank you for your comprehensive and detailed response.  I am confident that it will solve my issue. I am still working on the implementation.  Much appreciated!

Resources