Single Active CTRL Button to send email basedon Row

VBANEWBIE1229

New Member
Joined
Oct 19, 2017
Messages
5
Hi guys,

I am trying to send an email from excel 2013 based on the data in the spreadsheet. I currently have an active button on each row with a macro assigned. This works perfectly, however, I would like to be able to only have one button per sheet that will create the email based on the active selected row. The current code I'm using is below. Thanks in advance for your help!!!!!

Private Sub CommandButton21_Click()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = Range("E9")
.CC = Range("F9")
.BCC = ""
.Subject = "SUBJECT TEXT"
.Body = "Hello, " & vbNewLine & vbNewLine & "EMAIL TEXT"
.Attachments.Add (ActiveSheet.Range("C9").Value)
.Display
MsgBox ("Email Created")
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Please click the # icon on the toolbar and paste code between tags.

Here is an example:
Code:
'.Attachments.Add (ActiveSheet.Range("C9").Value)
.Attachments.Add .cells(activecell.Row, "C").value
 
Last edited:
Upvote 0
Thank you. This didn't work for me. What I am actually trying to do is have one button that will pull from 3 cells depending on which row is selected. My data is in colums C, E, & F. So if I have row 6 selected, I would like it to get the values from cells C6, E6 &F6.
 
Upvote 0
Did you not get that ActiveCell.Row returns the active cell's row value? I left it to you to change the others. Do you understand now?
 
Upvote 0
Thank you for your posts. VBA is very new for me, I understand...this is what I have so far for the rows that I need values.

.To = .Cells(ActiveCell.Row, "E")
.CC = .Cells(ActiveCell.Row, "F")
.Attachments.Add .Cells(.ActiveCell.Row, "C").Value
 
Upvote 0
Does that solve your issue?
Code:
Private Sub CommandButton21_Click()
  Dim OutApp As Object, OutMail As Object, r As Long
  
  r = ActiveCell.Row
  
  Set OutApp = CreateObject("Outlook.Application")
  OutApp.Session.Logon
  Set OutMail = OutApp.CreateItem(0)
  On Error Resume Next
  
  With OutMail
    .To = Cells(r, "E").Value2 'Range("E" & r)
    .CC = Cells(r, "F").Value2 'Range("F9")
    .BCC = ""
    .Subject = "SUBJECT TEXT"
    .Body = "Hello, " & vbNewLine & vbNewLine & "EMAIL TEXT"
    .Attachments.Add Cells(r, "C").Value2 '(ActiveSheet.Range("C9").Value)
    .Display
    MsgBox ("Email Created")
  End With
  
  On Error GoTo 0
  Set OutMail = Nothing
  Set OutApp = Nothing
End Sub

FWIW: Cells() does not enable intellisense but does allow either letter string or column number for the 2nd parameter, column. Range() has intellisense but because we have to use string concatenation for this case, it is a bit slower to execute if you do alot of those.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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