+ Reply to Thread
Results 1 to 5 of 5

Do Until Function returning Sub or Function not defined error

Hybrid View

Rokn Do Until Function returning... 11-28-2013, 01:38 PM
wakeupcall Re: Do Until Function... 11-28-2013, 02:51 PM
Rokn Re: Do Until Function... 11-28-2013, 04:26 PM
wakeupcall Re: Do Until Function... 11-29-2013, 11:43 AM
mikerickson Re: Do Until Function... 11-29-2013, 01:57 PM
  1. #1
    Registered User
    Join Date
    07-14-2013
    Location
    Grand Rapids, Michigan
    MS-Off Ver
    Excel 2010
    Posts
    35

    Do Until Function returning Sub or Function not defined error

    So I'm working on building a distribution list off of my schedule at work and I found some handy dandy vba code for outlook on youtube, but in the video he admits that his code will only look up for a specific range, and my list will change daily considering vacation time, sick time, etc. He then says, "So you'll have to figure a way around this."

    From some of my previous projects, I figured I could just do a:

    DoUntil IsEmpty.Activecell
    . . . but as it appears in the below code, it's giving me a Sub or Function Not Defined error. I'm hoping it's just a simple fix as I'm trying to keep the code as simple as possible.

    Sub SendEmail(what_address As String, subject_line As String, mail_body As String)
    
    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
    
    
    
    Private Sub CommandButton1_Click()
    '
    'set starting point under header
    
    row_number = 1
    '
    'run loop to send email to all identified recipients upon click
    
    Do
    DoUntil IsEmpty("A" & row_number)
     
        row_number = row_number + 1
        Call SendEmail(Sheet1.Range("A" & row_number), Sheet1.Range("B" & row_number), Sheet1.Range("C2"))
     
     Loop
    End Sub
    Any help you could provide would be much oblidged.

  2. #2
    Forum Contributor wakeupcall's Avatar
    Join Date
    02-29-2012
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    155

    Re: Do Until Function returning Sub or Function not defined error

    Try

     Do Until IsEmpty(Cells(row_number, "A"))
    Liked the answer given? click * to say so

  3. #3
    Registered User
    Join Date
    07-14-2013
    Location
    Grand Rapids, Michigan
    MS-Off Ver
    Excel 2010
    Posts
    35

    Re: Do Until Function returning Sub or Function not defined error

    So, it works like a charm . . . for that problem. I mean, it actually works now as far as sending the email, but because of the:

    Do Until IsEmpty(Cells(row_number, "A"))
    It actually tries to send one to the blank cell it stops on as an additional recipient and gives me this debug error:

    Run-time error ='2147467259 (80004005)'

    There must be at least one name or distribution list in the To, CC, or Bcc box

    I tried this:
    If OlMail.to = "" then stop
    If OlMail.Subject = "" then stop
    If OlMail.body = "" then stop
    But it's obvious this isn't the proper solution to anyone who's written in vba, I just don't think I know enough about it to problem solve on my own.

    I mean, it works, so if it were me sending the mass email, I would know to just debug and reset the macro each time I needed to send a new email, but I don't trust my peers to know how to do this . . . and ultimately, if I'm getting a debug error, it's really not a completed macro . . . since the whole point of writing a macro is to have smooth, seamless processes that are efficient.

    The other thing is that the only field that doesn't have data in it is the 'To' section. So even if the code was correct, which I'm sure it's not, I would probably only need to modify the:

    OlMail.to
    section.
    Last edited by Rokn; 11-28-2013 at 04:28 PM. Reason: mispelling, additional info

  4. #4
    Forum Contributor wakeupcall's Avatar
    Join Date
    02-29-2012
    Location
    Sofia, Bulgaria
    MS-Off Ver
    Excel 2010
    Posts
    155

    Re: Do Until Function returning Sub or Function not defined error

    Try the other syntax of Do until:

    Do Until condition
    action
    Loop

    Private Sub CommandButton1_Click()
    '
    'set starting point under header
    
    row_number = 1
    
    Do Until IsEmpty(Cells(row_number, "A"))
        row_number = row_number + 1
        Call SendEmail(Sheet1.Range("A" & row_number), Sheet1.Range("B" & row_number), Sheet1.Range("C2"))
    Loop 
     
    End Sub
    This will first check for instances matching the condition and then execute the action. It should prevent the macro from trying to send an e-mail to the blank cell it stops on

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Do Until Function returning Sub or Function not defined error

    Flip it around and Send the tested value before incrimentiong

    Row_Number = 1
    
    Do While Not(IsEmpty(Cells(Row_Number, "A"))
        Call SendEmai(....)
        Row_Number = Row_Number + 1
    Loop
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] User defined function returns an error on a standard function used in it.
    By pb48 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-23-2013, 01:35 PM
  2. Application Defined or Object Defined Error, Command Button and Vlookup Function
    By stevedomer in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-14-2011, 11:20 PM
  3. #NAME Error with Function Defined with Public Function
    By Simon Austin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-23-2009, 08:46 AM
  4. User defined function returning #VALUE!
    By rabson in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-22-2007, 10:50 AM
  5. [SOLVED] Excel - User Defined Function Error: This function takes no argume
    By BruceInCalgary in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-18-2006, 04:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1