How Can I get this functionality for all rows short of coding it for each instance?

franswa3434

Board Regular
Joined
Sep 16, 2014
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Hey all,

I created a script that will auto-generate an email in outlook based upon a cell value in a row, and will populate the email based upon information in that same row.

Long story short, I would like to be able to apply this to all rows, or at least to a defined set, and if possible, all without having to script it for each row.

Help?

Thank you all for your help!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)    
    Dim xRg As Range
    
    On Error Resume Next
    If ActiveSheet.Range("J2") = "Waiting for Response From Carrier" 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 = "Hello," & vbNewLine & vbNewLine & _
              "Would you please provide me a quote for the following address:" & vbNewLine & vbNewLine & _
              "Address: " & ActiveSheet.Range("F2") & ", " & ActiveSheet.Range("G2") & ", " & ActiveSheet.Range("H2") & " " & ActiveSheet.Range("I2") & vbNewLine & _
              "Service: " & ActiveSheet.Range("C2") & "Mb/" & ActiveSheet.Range("D2") & "Mb - " & ActiveSheet.Range("E2") & vbNewLine & vbNewLine & _
              "Thank you,"
  
    On Error Resume Next
    With xOutMail
        .SentOnBehalfOfName = "xxxxxxx@xxxxxxx.com"
        .To = ActiveSheet.Range("N2")
        .CC = ActiveSheet.Range("P2") & ";xxxxxxx@xxxxxxx.com;xxxxxxx@xxxxxxx.com"
        .BCC = ""
        .Subject = "Service Request - CW: " & ActiveSheet.Range("B2") & " - " & ActiveSheet.Range("F2") & ", " & ActiveSheet.Range("G2") & ", " & ActiveSheet.Range("H2") & " " & ActiveSheet.Range("I2")
        .Body = xMailBody
        .Display   'or use .Send
        
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hello franswa3434,

Try this adaptation of your macros.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


        ' The cell being changed must be in column "J" and the row must be 2 or greater.
        If Target.Column <> 10 Then Exit Sub
        If Target.row = 1 Then Exit Sub
        
        If Target = "Waiting for Response From Carrier" Then
            Call Mail_small_Text_Outlook(Target.row)
        End If
    
End Sub


Sub Mail_small_Text_Outlook(ByVal row As Long)


    Dim xOutApp     As Object
    Dim xOutMail    As Object
    Dim xMailBody   As String
    
        Set xOutApp = CreateObject("Outlook.Application")
        Set xOutMail = xOutApp.CreateItem(0)
        
        xMailBody = "Hello," & vbNewLine & vbNewLine & _
              "Would you please provide me a quote for the following address:" & vbNewLine & vbNewLine & _
              "Address: " & Cells(row, "F") & ", " & Cells(row, "G") & ", " & Cells(row, "H") & " " & Cells(row, "I") & vbNewLine & _
              "Service: " & Cells(row, "C") & "Mb/" & Cells(row, "D") & "Mb - " & Cells(row, "E") & vbNewLine & vbNewLine & _
              "Thank you,"
  
        With xOutMail
            .SentOnBehalfOfName = "xxxxxxx@xxxxxxx.com"
            .To = Cells(row, "N")
            .CC = Cells(row, "P") & ";xxxxxxx@xxxxxxx.com;xxxxxxx@xxxxxxx.com"
            .BCC = ""
            .Subject = "Service Request - CW: " & Cells(row, "B") _
                    & " - " & Cells(row, "F") _
                    & ", " & Cells(row, "G") _
                    & ", " & Cells(row, "H") _
                    & " " & Cells(row, "I")
            .Body = xMailBody
            .Display   'or use .Send
        End With
    
        Set xOutMail = Nothing
        Set xOutApp = Nothing
    
End Sub
 
Upvote 0
Hello franswa3434,

You're welcome. Glad I could help.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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