MasterBash
Board Regular
- Joined
- Jan 22, 2022
- Messages
- 70
- Office Version
- 365
- Platform
- 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 :
This is my VBA code :
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.
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 :
Cell Formulas | ||
---|---|---|
Range | Formula | |
B2:B25,B27:B29 | B2 | ="Country of client "&$A2 |
C2:C25,C27:C29 | C2 | ="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.