Optimizing my rows to send e-mail to different address

MasterBash

Board Regular
Joined
Jan 22, 2022
Messages
62
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to optimize my code that I use to send emails by clicking text in column C.

I want rows 2 to 25 (client A to X) to send to "test" and 27 to 29 (client 1 to 3) to send to "test123". Column A, B and C are all in a table, so we can easily expand the list if we choose to do so.

This is my current sheet :

email test.xlsm
ABCDEFG
1ClientCountryEmailEmailtest
2ACountry of client ASend e-mail to Country of client ASubjectREQUEST…
3BCountry of client BSend e-mail to Country of client BBodyPlease provide…
4CCountry of client CSend e-mail to Country of client C
5DCountry of client DSend e-mail to Country of client Dclient A to X send email to test
6ECountry of client ESend e-mail to Country of client E
7FCountry of client FSend e-mail to Country of client F
8GCountry of client GSend e-mail to Country of client G
9HCountry of client HSend e-mail to Country of client H
10ICountry of client ISend e-mail to Country of client I
11JCountry of client JSend e-mail to Country of client J
12KCountry of client KSend e-mail to Country of client K
13LCountry of client LSend e-mail to Country of client L
14MCountry of client MSend e-mail to Country of client M
15NCountry of client NSend e-mail to Country of client N
16OCountry of client OSend e-mail to Country of client O
17PCountry of client PSend e-mail to Country of client P
18QCountry of client QSend e-mail to Country of client Q
19RCountry of client RSend e-mail to Country of client R
20SCountry of client SSend e-mail to Country of client S
21TCountry of client TSend e-mail to Country of client T
22UCountry of client USend e-mail to Country of client U
23VCountry of client VSend e-mail to Country of client V
24WCountry of client WSend e-mail to Country of client W
25XCountry of client XSend e-mail to Country of client X
26
271Country of client 1Send e-mail to Country of client 1Emailtest123
282Country of client 2Send e-mail to Country of client 2SubjectREQUEST…
293Country of client 3Send e-mail to Country of client 3BodyPlease provide…
30
31client 1 to 3 send email to test123
Email client
Cell Formulas
RangeFormula
B2:B25,B27:B29B2="Country of client "&$A2
C2:C25,C27:C29C2="Send e-mail to "&B2


This is my VBA code :

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim location As String
    Dim subject As String
    Dim body As String
    Dim email As String
    Dim Cell As Range
    Dim TargetRow

If Intersect(Target, UsedRange) Is Nothing Then Exit Sub
  
   For Each TargetRow In Intersect(Target, UsedRange).Rows

      If TargetRow.Columns.Count = 1 And TargetRow.Column = 3 Then ' column C is the only column selected in the row
         Set Cell = TargetRow
         location = Range("B" & Cell.Row).Value
     
         If Cell.Row <= 25 And Cell.Column = 3 And Cell <> "" Then

           email = Range("G1")
           subject = Range("G2") & location
           body = Range("G3") & location
          
        ElseIf Cell.Row >= 26 And Cell.Column = 3 And Cell <> "" Then
       
           email = Range("G27")
           subject = Range("G28") & " " & location
           body = Range("G29") & location
          
    If subject <> "" Then
        Send_The_Emails subject, body, email
    End If
    End If
    End If
    Next TargetRow
End Sub

I currently have 2 problems with this code. First, it does not work for client A to X (2 to 25) and second it is not modular enough. If possible, I would like to have ranges in cells. 2 to 25 will send to test and 27 to 29 to test123, and we will be able to modify those if we add more clients. In other words, instead of using Cell.Row <= or >=, I would prefer to have something that my team can easily modify if they were to add new rows.

How can I do that ?

Thank you.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi MasterBash
here is a code example, assuming that there is always an empty row (only one) in the table between the two groups. This way you can use a different instruction depending on whether the click is made in the first group, before the empty row, or in the second group, after the empty row. To adapt the name of the table in the code
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim tbl As ListObject
    Dim emptyRow As Range
    Dim emptyRowIndex As Long

    ' Disabilita gli eventi per evitare loop infiniti
    Application.EnableEvents = False
    
    ' Set your table
    Set tbl = Me.ListObjects("Table1") '<<==== ADAPT table name as needed
    
    ' Find first empty row
    On Error Resume Next
    Set emptyRow = tbl.ListColumns(3).DataBodyRange.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0

    ' If an empty row is found
    If Not emptyRow Is Nothing Then
        emptyRowIndex = emptyRow.Row ' Ottieni l'indice di riga della riga vuota
        
        ' column C is the only column selected in the row
        If Not Intersect(Target, tbl.ListColumns(3).DataBodyRange) Is Nothing Then
            ' Single cell selected
            If Target.Cells.Count = 1 Then
                ' If selected row is above the blank row
                If Target.Row < emptyRowIndex Then
                 ' <<< ==== first case   
                ' If selected row is under the blank row
                ElseIf Target.Row > emptyRowIndex Then
                   ' <<< ==== second case
                Else
                    'empty row DO nothing
                End If
            End If
        End If
    End If

    ' Riabilita gli eventi
    Application.EnableEvents = True
End Sub

However I think that this is not the best organization of the data, it would be better to insert a unique value in a column that identifies the group
 
Upvote 0
Solution
Yes, thank you ! I will be using a column that identifies the group. That seems to be the best option.
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,969
Members
452,539
Latest member
delvey

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