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
 

Some videos you may like

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,350
Messages
5,528,191
Members
409,807
Latest member
nicky736

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top