Multiple cell change email notifications

Sven62

Active Member
Joined
Feb 21, 2012
Messages
485
The VBA bellow will send an email to a specific person when the given cell range is changed. I need to make it email different people for different ranges. So if C2:C50 changes, then certain person(s) including CC and BCC gets an email. If D2:D50 changes, then a different person(s) get an email, including CC and BCC. I tried simply adding a new Private Sub with a new name ("Worksheet_Change2") and different cell range and recipient list... that didn't work.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    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("D2:D50")
    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 = "xxxxx@xxxxxx.com"
            .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
 
You no longer have "xRgSel" defined in your code, but you are trying to reference it.
You actually don't need it. Just replace "xRgSel" with "Target".

I am also unclear what "Me.Name" is supposed to represent. That may not be necessary either, or might need some editing.
Replacing xRegSel with Target fixed it. Me.Name makes it insert the name of the worksheet in the body text message. That maybe antiquated and there might be a better way to reference it, but it does work. Thank you!
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Excellent!
You are welcome.
Glad we were able to get it working!
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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