+ Reply to Thread
Results 1 to 9 of 9

Macro That Sends Email to Address in Column B When Data is Added to Column S

  1. #1
    Registered User
    Join Date
    06-11-2018
    Location
    Montreal, QC
    MS-Off Ver
    Microsoft Excel 2016 MSO 32-bit
    Posts
    7

    Macro That Sends Email to Address in Column B When Data is Added to Column S

    Hi,

    I have a spreadsheet generated by Microsoft Forms that updates information in new rows everytime the form is submitted.

    Column C contains the submitter's email address. When the request has been completed, I enter the "Date Completed" in Column S. When I enter this information, I want it to send an email to the email address located in Column B. The Subject needs to be the information in Column H and the body needs to include information from Column T.

    I also want the loop to go from i = 2 to the last used cell but do not know how to reference this.

    This is what I have so far but no matter where I fill out the information in Column S, it only takes the information from row 2. If the date is entered in S15, for example, then it needs to use the email address from C15 and the information from H15 and T15. Is there a way to do this??

    This is what I have so far (I used i = 2 to 1000 arbitrarily because I could not figure this part out). It works this way, but then it takes way too long to finish the loop and sometimes freezes Excel. Is there a way to shorten this range for something like i = 2 to (Last Used Row).


    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("S:S")
    Set xRgSel = Intersect(Target, xRg)
    ActiveWorkbook.Save

    For i = 2 To 1000
    If Not xRgSel Is Nothing Then
    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailItem = xOutApp.CreateItem(0)
    xMailBody = "Purification " & Cells(i, 8).Value & " is Complete." & vbNewLine & vbNewLine & "The following method was used: " & Cells(i, 20).Value
    With xMailItem
    .To = Cells(i, 3).Value
    .Subject = "Purification " & Cells(i, 8).Value & " is Complete"
    .Body = xMailBody
    .Display
    End With
    Set xRgSel = Nothing
    Set xOutApp = Nothing
    Set xMailItem = Nothing
    End If
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Next

    End Sub


    Thanks for your insight!

  2. #2
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Macro That Sends Email to Address in Column B When Data is Added to Column S

    Instead of 1000, use this in the line to find the last cell:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-11-2018
    Location
    Montreal, QC
    MS-Off Ver
    Microsoft Excel 2016 MSO 32-bit
    Posts
    7

    Re: Macro That Sends Email to Address in Column B When Data is Added to Column S

    Thanks! This sped up the macro quite a bit! Now, just to get it to do what I want it to!

  4. #4
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,010

    Re: Macro That Sends Email to Address in Column B When Data is Added to Column S

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-11-2018
    Location
    Montreal, QC
    MS-Off Ver
    Microsoft Excel 2016 MSO 32-bit
    Posts
    7

    Re: Macro That Sends Email to Address in Column B When Data is Added to Column S

    Quote Originally Posted by Logit View Post
    Please Login or Register  to view this content.
    I tried this and it generated emails for every single row in the spreadsheet when the value was changed in column S. I need it so that an email is only generated for the row that changed. For example, if S15 is changed, the only email generated should be from the data in row 15.

    Is this possible?

  6. #6
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,010

    Re: Macro That Sends Email to Address in Column B When Data is Added to Column S

    .
    Will an email be sent only once when the Col S value changes ?

    Or are you expecting anytime the Col S value changes, even if it had changed previously, an email will be sent ?

  7. #7
    Registered User
    Join Date
    06-11-2018
    Location
    Montreal, QC
    MS-Off Ver
    Microsoft Excel 2016 MSO 32-bit
    Posts
    7

    Re: Macro That Sends Email to Address in Column B When Data is Added to Column S

    The email will only be sent once. Basically the cell in column S is empty and when this item is completed, I enter the "Date Complete" in Column S, and then it should send the email.

  8. #8
    Registered User
    Join Date
    06-11-2018
    Location
    Montreal, QC
    MS-Off Ver
    Microsoft Excel 2016 MSO 32-bit
    Posts
    7

    Re: Macro That Sends Email to Address in Column B When Data is Added to Column S

    I found a slightly different way and it worked!

    When I enter the date and press enter, the Active Cell is in the row below the target row so I just used Cells(ActiveCell.Row - 1, "Column Number") to fetch the data.

    Thanks for all of your input!

    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("S:S")
    Set xRgSel = Intersect(Target, xRg)

    ActiveWorkbook.Save


    If Not xRgSel Is Nothing Then
    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailItem = xOutApp.CreateItem(0)
    xMailBody = "Purification " & Cells(ActiveCell.Row - 1, 8).Value & " is Complete." & vbNewLine & vbNewLine & "The following method was used: " & Cells(ActiveCell.Row - 1, 20).Value

    With xMailItem
    .To = Cells(ActiveCell.Row - 1, 3).Value
    .Subject = "Purification " & Cells(ActiveCell.Row - 1, 8).Value & " is Complete"
    .Body = xMailBody
    .Display
    End With
    Set xRgSel = Nothing
    Set xOutApp = Nothing
    Set xMailItem = Nothing
    End If
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    End Sub

  9. #9
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,010

    Re: Macro That Sends Email to Address in Column B When Data is Added to Column S

    .
    You need to place your posted code inside the # symbol, selected from the REPLY menu bar. Please edit your last post.

    I edited your last posted macro, removing the -1 from all lines where present and the code functions as desired here.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] macro to send mail from excel - sends two mails - automatic signature to be added
    By excellon in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 12-05-2019, 05:22 AM
  2. [SOLVED] pull email address from a column based on criteria from another column
    By cinstanl in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-09-2016, 08:09 AM
  3. Copy email address in column dependant on another column
    By N1k100 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 03-31-2015, 10:09 AM
  4. Pulling email address from irrelevant data within cell and inserting in adjacent column
    By SouthFloridaBadger in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-05-2013, 04:28 PM
  5. [SOLVED] Extracting email chain email address and converting to Excel column
    By Leah_Hael in forum Excel General
    Replies: 4
    Last Post: 12-28-2012, 04:05 PM
  6. can I copy a column of email addresses, paste into email address?
    By Lizizfree in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 07-20-2006, 05:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1