I said in a comment, it's probably easier to simply color cells to look like buttons and have the users click on a cell to send the emails - then you can simply use the offset for the particular row, but if you insist on using command buttons, it's quite simple.
Take your current code and put it in a new subroutine that accepts a range parameter.
Then, add your buttons, and link each one to its own code with a different range.
Option Explicit
Private Sub CommandButton3_Click()
SendEmail Range("A3")
End Sub
Private Sub CommandButton4_Click()
SendEmail Range("A4")
End Sub
Private Sub CommandButton5_Click()
SendEmail Range("A5")
End Sub
`...
Sub SendEmail(TheRange as Range)
On Error GoTo ErrHandler
' SET Outlook APPLICATION OBJECT.
Dim objOutlook As Object
Set objOutlook = CreateObject("Outlook.Application")
' CREATE EMAIL OBJECT.
Dim objEmail As Object
Set objEmail = objOutlook.CreateItem(olMailItem)
With objEmail
.to = "[email address].com"
.Subject = TheRange 'Change this line
.Body = "[Message]"
.Send ' SEND THE MESSAGE.
End With
' CLEAR.
Set objEmail = Nothing: Set objOutlook = Nothing
ErrHandler:
End Sub
If you prefer instead to use the SelectionChanged
event, you can do it like this.
Then, you can just update [C4:C8]
if you want to add any more "buttons"
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Not Intersect(Target, [C4:C8]) Is Nothing Then
SendEmail Range("A" & Target.Row)
'Optionally select the subject we sent so we can re-click
'You can choose any other cell not in our event range
Range("A" & Target.Row).Select
End If
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…