VBA to send email to recipient list if a cell/cells change, trigger on file save?

Guitarmageddon

Board Regular
Joined
Dec 22, 2014
Messages
159
Hello folks, just like the subject states. Trying to figure out a way if I can have VBA that monitors a particular cell or cells for changes, and that check is executed upon file saving. If there is a change, it would email a preset list of contacts in outlook. Perhaps that email could even have a link to the file? Or maybe, just the file extension itself is ok. Im flexible on that. Does this sound doable? The file was kept on share point but I think the range of options for triggering emails when conditions are met isnt as granular as "look in the file at a particular cell" but rather looking at modified time stamps or something.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
VBA Code:
Sub check_cell_for_changes()
    
    Dim ws          As Worksheet
    Dim cell_range  As Range
    Dim old_value   As Variant
    
    Set ws = ActiveSheet
    Set cell_range = Range("A1")
    old_value = cell_range.Value
    
    Application.EnableEvents = TRUE
    
    Do While TRUE
        If cell_range.Value <> old_value Then
            Call send_email
        End If
        
        DoEvents
        old_value = cell_range.Value
        Application.Wait (Now + TimeValue("0:00:05"))
        
    Loop
    
End Sub

Private Sub send_email()
    Dim olApp       As Outlook.Application
    Dim myMail      As Outlook.MailItem
    Set olApp = CreateObject("Outlook.Application")
    Set myMail = olApp.CreateItem(olMailItem)
    
    myMail.Subject = "Cell A1 Changed"
    myMail.Body = "The value of cell A1 has changed."
    myMail.To = "recipient@example.com"
    myMail.Send
    
End Sub
 
Upvote 0
VBA Code:
Sub check_cell_for_changes()
   
    Dim ws          As Worksheet
    Dim cell_range  As Range
    Dim old_value   As Variant
   
    Set ws = ActiveSheet
    Set cell_range = Range("A1")
    old_value = cell_range.Value
   
    Application.EnableEvents = TRUE
   
    Do While TRUE
        If cell_range.Value <> old_value Then
            Call send_email
        End If
       
        DoEvents
        old_value = cell_range.Value
        Application.Wait (Now + TimeValue("0:00:05"))
       
    Loop
   
End Sub

Private Sub send_email()
    Dim olApp       As Outlook.Application
    Dim myMail      As Outlook.MailItem
    Set olApp = CreateObject("Outlook.Application")
    Set myMail = olApp.CreateItem(olMailItem)
   
    myMail.Subject = "Cell A1 Changed"
    myMail.Body = "The value of cell A1 has changed."
    myMail.To = "recipient@example.com"
    myMail.Send
   
End Sub
Hello,
This just spun for a while and I ended up having to kill excel. Not sure if there is any other prep with the VBA settings I needed to do?

Another thing Im curious about is how this activates. Can it be activated upon file save? Or, does it always require a manual intervention of someone to run the macro?
 
Upvote 0
I have found this code works on running (currently with just the criteria dummied in of "does xyz cell not equal some number"


However, Im looking for two additonal features.
1)some way to run this without the user executing the macro (i would like to do it on file save)
2)can the email be told to indicate what cell has changed?

VBA Code:
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
 
Upvote 0
Coming back to this to bump. I think the key may actual be a different mechanism to find the changes. how about the file having two sheets: "Sheet1" and a second "hidden reference" for example. Then, the code can execute upon file save and check to compare the hidden sheet. If it spots differences, it notes those, puts that data in an email i.e. "A5 changed to 'completed status' " or something to that effect. Once the email is created, it then copies the data into the hidden reference sheet, and then the file can be closed. Thoughts?
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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