Need to repeat VBA code down rows - at the moment inflexible

indydc

New Member
Joined
May 23, 2019
Messages
2
Let me start by saying I am completely new to VBA as of today so apologies in advance, I won't have any advanced understanding.

I managed to find a VBA code online, in order to help me create automated emails.

In essence, the below code is picking up the desired email addresses in cell B2, and the mail body is the contents of cell C2. And it is all kicked off if the value entered into A2 is more than 0.
But for the life of me, I cannot make this flexible so that it does not apply to row 2. I want this to happen for row 3 (email address from B3, mail content of C3 [which is personalised for names] if the value entered into A3 is more than 0), and then row 4, row 5 etc. Could someone help me please?



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("A2"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value > 0 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 = Range("C2")
On Error Resume Next
With xOutMail
.To = Range("B2")
.CC = ""
.BCC = ""
.Subject = "Engaging our Clients on Sustainability"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Let me start by saying I am completely new to VBA as of today so apologies in advance, I won't have any advanced understanding.

I managed to find a VBA code online, in order to help me create automated emails.

In essence, the below code is picking up the desired email addresses in cell B2, and the mail body is the contents of cell C2. And it is all kicked off if the value entered into A2 is more than 0.
But for the life of me, I cannot make this flexible so that it does not apply to only row 2. I want this to happen for row 3 (email address from B3, mail content of C3 [which is personalised for names] if the value entered into A3 is more than 0), and then row 4, row 5 etc. Could someone help me please?



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("A2"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value > 0 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 = Range("C2")
On Error Resume Next
With xOutMail
.To = Range("B2")
.CC = ""
.BCC = ""
.Subject = "Engaging our Clients on Sustainability"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub

Edited - want it to apply to more rows (around 180 in total), not just row 2 like it is doing at the moment.
 
Upvote 0
Welcome to the forum.

Try this

Code:
'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
[COLOR=#0000ff]    If Not Intersect(Target, Range("A:A")) Is Nothing Then[/COLOR]
        If IsNumeric(Target.Value) And Target.Value > 0 Then
[COLOR=#0000ff]            Call Mail_small_Text_Outlook(Target.Row)[/COLOR]
        End If
    End If
End Sub


Sub Mail_small_Text_Outlook([COLOR=#0000ff]r[/COLOR])
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = Range([COLOR=#0000ff]"C" & r[/COLOR])
    On Error Resume Next
    With xOutMail
        .To = Range([COLOR=#0000ff]"B" & r[/COLOR])
        .CC = ""
        .BCC = ""
        .Subject = "Engaging our Clients on Sustainability"
        .Body = xMailBody
        .Display 'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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