Call Macro in VBA

jpprespa

New Member
Joined
Apr 10, 2013
Messages
8
Hello,

I have two macro's. One called SendEmail and one called Sendmassemail. In Sendmassemail, I would like to call SendEmail, but am getting an error 'Expected Variable or Procedure, not module'.

Here's my code

Sub Sendmassemail()


row_number = 1


Do
DoEvents
row_number = row_number + 1

Call SendEmail(Sheet1.Range("A" & row_number), "This is a test e-mail", Sheet1.Range("J2"))

lastRow = Range("A1:A").End(xlUp).Row + 1

Loop Until lastRow


End Sub

I checked the name of my module and it's the same. Am I calling this incorrectly?

Joe
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
You need to rename the module to something other than any subs that you have.
 

jpprespa

New Member
Joined
Apr 10, 2013
Messages
8
You need to rename the module to something other than any subs that you have.

Ok, I changed that. Now i get an error 'Object variable or With block variable not set' on 'Set olMail = olApp.CreateItem(olMailItem)'. Sorry, new to this.

Sub SendEmail(what_address As String, subject_line As String, mail_body As String)


Dim olApp As Outlook.Application
Set o1App = CreateObject("Outlook.Application")


Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)

olMail.To = what_address
olMail.Subject = subject_line
olMail.Body = mail_body
olMail.Send

End Sub
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Typo

Rich (BB code):
Set olApp = CreateObject("Outlook.Application")
 

jpprespa

New Member
Joined
Apr 10, 2013
Messages
8

ADVERTISEMENT

Typo

Rich (BB code):
Set olApp = CreateObject("Outlook.Application")

Ok, one last one. I'm getting a Type Mismatch error with my loop. I want to loop through the email addresses in column A until I reach the last row.

Sub SendMassEmail()
Dim lastRow As Long


row_number = 1


Do
DoEvents
row_number = row_number + 1
Call SendEmail(Sheet1.Range("A" & row_number), "This is a test e-mail", Sheet1.Range("J2"))
lastRow = Sheet1.Range("A99999").End(xlUp).Row + 1
Loop Until lastRow = ""


End Sub
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Sub SendMassEmail()
Dim lastRow As Long, i As Long
lastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow
    Call SendEmail(Sheet1.Range("A" & row_number).Value, "This is a test e-mail", Sheet1.Range("J2").Value)
Next i
End Sub
 

jpprespa

New Member
Joined
Apr 10, 2013
Messages
8
That works great. I change .send to .display so that I could look at the output. For some reason my macro is opening another workbook rather than using the Active workbook when populating the email. I don't have any other workbooks open.

Thanks for your help, you've helped debug most of this.


Try

Code:
Sub SendMassEmail()
Dim lastRow As Long, i As Long
lastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow
    Call SendEmail(Sheet1.Range("A" & row_number).Value, "This is a test e-mail", Sheet1.Range("J2").Value)
Next i
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,651
Messages
5,838,582
Members
430,557
Latest member
MK15

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
Top