VBA Code Issue

KFerguson84

New Member
Joined
Jan 19, 2016
Messages
4
Hello.

I am trying to get the below code to run to open an email addressed to email addresses that populate in a cell based on a VLOOKUP.

The code is

[Private Sub CommandButton21_Click()Sub SendingSheet()


Dim oMSOutlook As Object
Dim oEmail As Object


Set oMSOutlook = CreateObject("Outlook.Application")
Set oEmail = oMSOutlook.CreateItem(olMailItem)


With oEmail
.To = ActiveSheet.Cells(1, 1)
.CC = ActiveSheet.Cells(2, 1)
.BCC = ActiveSheet.Cells(3, 1)
.Send
End With




Set oMSOutlook = Nothing
Set oEmail = Nothing


End Sub]

When I run it, I get an error that reads:

Compile Error:

Expected End Sub

I am good at Excel but new to Visual Basic. I'm hoping someone can tell me what I am doing to cause the error and point me in the right direction.

Thank you!!!!
 
Here is a repost of the code. It didn't format correctly when I copied it.


Code:
Private Sub CommandButton21_Click()
Sub SendingSheet()


Dim oMSOutlook As Object
Dim oEmail As Object


Set oMSOutlook = CreateObject("Outlook.Application")
Set oEmail = oMSOutlook.CreateItem(olMailItem)


With oEmail
.To = ActiveSheet.Cells(1, 1)
.CC = ActiveSheet.Cells(2, 1)
.BCC = ActiveSheet.Cells(3, 1)
.Send
End With




Set oMSOutlook = Nothing
Set oEmail = Nothing


End Sub
 
Last edited by a moderator:
Upvote 0
Code:
Private [COLOR=#ff0000]Sub[/COLOR] CommandButton21_Click()
[COLOR=#ff0000]Sub[/COLOR] SendingSheet()

did you mean

Code:
Private Sub CommandButton21_Click()
SendingSheet
 
Upvote 0
mole999,
I changed the code to what you posted above, now I am getting a different error that says:

Compile Error:

Sub or Function not defined
 
Upvote 0
What is the line "Sub SendingSheet()"? You have a sub defined within another sub.
 
Upvote 0
My knowledge in VBA is about as limited as possible. I understand the functionality but I have no idea how the write the code to perform that functionality. I am looking for a code that I can attach to an ActiveX button that, when clicked, will copy an email address from one cell into the "To:" bar of an Outlook email, and copy the data from another cell into the "BCC:" bar of the same email. I would also like to pull data from a third cell into the subject line.

Can anyone assist with a code that would achieve this? I greatly appreciate the help!
 
Upvote 0

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