+ Reply to Thread
Results 1 to 6 of 6

Skip row in For...Next loop to automate e-mails

Hybrid View

  1. #1
    Registered User
    Join Date
    03-01-2012
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2010
    Posts
    64

    Skip row in For...Next loop to automate e-mails

    Hello folks

    I'm hoping this is a quick fix but I cannot get this to work and it's driving me mad. I'm adapting some code I found to automate sending e-mails from an Excel file. So far that works fine. I've then added a column to say 'Yes' once an e-mail has been sent so that the macro doesn't keep sending e-mails (FYI once this code is fully working I'll use Task Scheduler to run the procedure every day, hence the need to check off rows that have had an e-mail previously). This works fine also.

    What I want to do now though is tell this macro to skip a row if an e-mail has been sent and move to the next row, and I cannot get this to work. What piece of code do I need to do this? Any help is very much appreciated, I've attached my Excel file and the code I'm using is below:

    Sub Check_Tasks()
         
        Dim lastRow As Long, r As Long
         
        With Sheets("todo")
            lastRow = Cells(Rows.Count, "B").End(xlUp).Row
            For r = 5 To lastRow
                If Cells(r, "I").Value = "Yes" Then 'How do I skip and go to next line that doesn't contain a 'Yes' in column I?
                             
                If Cells(r, "E").Value = Date Then Send_Outlook_Email "Task Reminder", .Cells(r, "H").Value, .Cells(r, "G").Value
               
                Cells(r, "I").Value = "Yes"
                
            Next r
        End With
         
    End Sub
     
     
    Private Sub Send_Outlook_Email(subject As String, body As String, ToEmail As String)
         
        Dim OutlookApp As Object 'Outlook.Application
        Dim objMail As Object 'Outlook.MailItem
         
        On Error Resume Next
        Set OutlookApp = GetObject(, "Outlook.Application")
        On Error GoTo 0
        If OutlookApp Is Nothing Then Set OutlookApp = CreateObject("Outlook.Application")
         
        Set objMail = OutlookApp.CreateItem(0) 'olMailItem
        objMail.subject = subject
        objMail.body = body
        objMail.To = ToEmail
        objMail.Send
         
    End Sub
    Thanks
    Kenny
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    10-26-2008
    Location
    Birmingham, UK
    MS-Off Ver
    All versions up to 2010
    Posts
    1,025

    Re: Skip row in For...Next loop to automate e-mails

    Hi
    Try this code:

    Sub Check_Tasks()

    Dim lastRow As Long, r As Long

    With Sheets("todo")
    lastRow = Cells(Rows.Count, "B").End(xlUp).Row
    For r = 5 To lastRow
    If Cells(r, "I").Value = "Yes" Then 'How do I skip and go to next line that doesn't contain a 'Yes' in column I?
    Next r
    End If
    If Cells(r, "E").Value = Date Then Send_Outlook_Email "Task Reminder", .Cells(r, "H").Value, .Cells(r, "G").Value

    Cells(r, "I").Value = "Yes"

    Next r
    End With

    End Sub

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Skip row in For...Next loop to automate e-mails

    @ ARGK

    Just a reminder that code should be enclosed in code tags so that formatting makes it easier to read. You have two Next r statements with only one For r. That would probably have been spotted with indentation in place, as well as the fact that your End If is out of position.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    Kenny

    A simple If should do the trick.

    Sub Check_Tasks()
    Dim lastRow As Long, r As Long
         
       With Sheets("todo")
           lastRow = .Cells(Rows.Count, "B").End(xlUp).Row
           For r = 5 To lastRow
    
              ' if column I not 'Yes' send email
              If .Cells(r, "I").Value <> "Yes" Then 
                             
                 If Cells(r, "E").Value = Date Then 
                    Send_Outlook_Email "Task Reminder", .Cells(r, "H").Value, .Cells(r, "G").Value
                     .Cells(r, "I").Value = "Yes"
                 End If
    
              End If
    
          Next r
       End With
         
    End Sub
    If posting code please use code tags, see here.

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Skip row in For...Next loop to automate e-mails

    Hi,

    Sub Check_Tasks()
    
        Dim lastRow As Long, r As Long
    
        With Sheets("todo")
            lastRow = Cells(Rows.Count, "B").End(xlUp).Row
            For r = 5 To lastRow
                If LCase(Cells(r, "I").Value) <> "yes" Then    ' 'Skips when col I doesn't contain a 'Yes'
                    If Cells(r, "E").Value = Date Then Send_Outlook_Email "Task Reminder", .Cells(r, "H").Value, .Cells(r, "G").Value
                    Cells(r, "I").Value = "Yes"
                End If
            Next r
        End With
    
    End Sub

  6. #6
    Registered User
    Join Date
    03-01-2012
    Location
    Nottingham, England
    MS-Off Ver
    Excel 2010
    Posts
    64

    Re: Skip row in For...Next loop to automate e-mails

    ARGK, Norie and Richard

    Thanks all of you for responding so quickly, I did try adding a 'Next r' to my code but wasn't wrapping that in an 'End If' statement so I feel I was on the right lines at least! Kudos to this forum and it's collection of excellent, informative contributors

    Kenny

+ 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] Skip blank cells in a loop
    By k64 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-14-2013, 02:29 PM
  2. Use a loop but skip a row
    By amartino44 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2013, 12:26 PM
  3. Using Excel to automate e-mails sent from Outlook?
    By Shelter417 in forum Excel General
    Replies: 3
    Last Post: 02-26-2012, 03:25 PM
  4. How to skip special cases in a For Each loop
    By smohyee in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-16-2011, 02:20 PM
  5. [SOLVED] Loop through email address list to send e-mails
    By Paul. in forum Excel General
    Replies: 1
    Last Post: 04-12-2005, 08:06 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