+ Reply to Thread
Results 1 to 5 of 5

Loop to send Emails with Cell range issues

  1. #1
    Registered User
    Join Date
    09-26-2019
    Location
    Murray, UT
    MS-Off Ver
    365
    Posts
    3

    Loop to send Emails with Cell range issues

    Hi Friends,

    I'm having problems with the below code. I'm trying to set up a loop to send emails based on cell values. The email To and Body are also based on cell values, which is where my problems exist. I get errors each time it gets to the line with the value for the email address.

    I really have no idea what I'm doing, I got most of this code from another source which I would reference, but don't remember...

    Any help identifying the issues is most appreciated.

    -jawkkp

    Please Login or Register  to view this content.
    Last edited by Leith Ross; 09-26-2019 at 07:53 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Loop to send Emails with Cell range issues

    Hello jawkkp,

    I have rewritten the macro. The email addresses are in "AT4:AT27". The email body is in cells "AU4:AU27".

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    09-26-2019
    Location
    Murray, UT
    MS-Off Ver
    365
    Posts
    3

    Re: Loop to send Emails with Cell range issues

    This works great. Thank you so so so much.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Loop to send Emails with Cell range issues

    Hello jawkkp,

    You're welcome. Here is a description of the problems I found.

    There was a LOT wrong with the code you posted. Rewriting it correctly was easier. Indenting when you code makes it much easier to spot problems and to read. Once I did this step it was easy to see the variables for the subject, recipient (mail to address), and email body had been declared twice and the names were different. For instance, you have Mail_Subject and MailSub define. A good way of preventing duplicate variables or using variables that have not been declared is to include Option Explicit at the very top of all code in the module.

    A really big issue was this line in the For Next loop...
    Please Login or Register  to view this content.
    This changed the value of every cell in the range "AT4:AT27" to a "Y". Making all the information in your email equal "Y": Subject, Recipient, and Body.

    Generally speaking, if your code generates an error then there is a problem you need to address. It is a very bad practice to ignore the error like using On Error Resume Next with no error handler. Trapping the error and alerting the user and then exiting is a good strategy. You should always include the error number as well as the description. Error descriptions are not unique but error numbers are. The Error number can also help you identify the software component where the error occurred. Here is a example of how to do it.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-26-2019
    Location
    Murray, UT
    MS-Off Ver
    365
    Posts
    3
    Good advice. I really had no idea what I was doing, so I appreciate your help



    Quote Originally Posted by Leith Ross View Post
    Hello jawkkp,

    You're welcome. Here is a description of the problems I found.

    There was a LOT wrong with the code you posted. Rewriting it correctly was easier. Indenting when you code makes it much easier to spot problems and to read. Once I did this step it was easy to see the variables for the subject, recipient (mail to address), and email body had been declared twice and the names were different. For instance, you have Mail_Subject and MailSub define. A good way of preventing duplicate variables or using variables that have not been declared is to include Option Explicit at the very top of all code in the module.

    A really big issue was this line in the For Next loop...
    Please Login or Register  to view this content.
    This changed the value of every cell in the range "AT4:AT27" to a "Y". Making all the information in your email equal "Y": Subject, Recipient, and Body.

    Generally speaking, if your code generates an error then there is a problem you need to address. It is a very bad practice to ignore the error like using On Error Resume Next with no error handler. Trapping the error and alerting the user and then exiting is a good strategy. You should always include the error number as well as the description. Error descriptions are not unique but error numbers are. The Error number can also help you identify the software component where the error occurred. Here is a example of how to do it.

    Please Login or Register  to view this content.

+ 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. Send diferent emails based on range value
    By Fandrinella in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-14-2019, 09:03 AM
  2. [SOLVED] Help in loop to send emails
    By amitmodi_mrt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-01-2017, 04:29 AM
  3. Export Excel sheets and send them as emails (Loop)
    By alexander.gavrailov in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-08-2017, 09:09 AM
  4. Excel vba to auto-send customer emails (duplicate emails issue)
    By nadz84 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-29-2015, 10:08 AM
  5. Send Emails once expiry date is reached, and generate report based on emails sent
    By demonicscorpion in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-18-2014, 05:36 AM
  6. [SOLVED] Loop through rows/columns H and K in filtered table and send emails - Help Please Gurus ;)
    By Alina1404 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-30-2012, 03:23 AM

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