+ Reply to Thread
Results 1 to 8 of 8

Repeat macro for next row

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-08-2011
    Location
    London
    MS-Off Ver
    Work Office 365 Home 2019
    Posts
    428

    Repeat macro for next row

    Hi Team,

    I've got a macro that will create an email based on the active cell and inputting relative content in to the email from the worksheet

    As it stands, it only works on the activecell. How would I change my coding so that it does the active cell, then goes to the next row (same column) and repeats until the end.

    Here's the code (I've had to strip out some of the HTML coding)

    Sub Training_Enrolment_email()
    ' Working in Office 2000-2016
        Dim OutApp As Object
        Dim OutMail As Object
        Dim strbody As String
    
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
    
        strbody = "Dear " & ActiveCell.Offset(, -2) & ",<br/><br/>" & _
    "This is an email<br/><br/>" & _
    "In the body of the email will be <b>Colours for Deadline dates and other formatting<br/><br/>" & _
    "GOOD LUCK and enjoy the training!<br/><br/>" & _
    "Kindest Regards,"
    
        On Error Resume Next
    
        With OutMail
            .Display
            .SentOnBehalfOfName = "Name of mailbox"
            .To = ActiveCell
            .CC = ""
            .BCC = ""
            .Subject = "Mandatory Training"
            .HTMLBody = strbody & "<br>" & .HTMLBody
            .Display '.Send will be used
        
        End With
    
        On Error GoTo 0
        Set OutMail = Nothing
        Set OutApp = Nothing
        
        'Turn back on screen updating
        Application.ScreenUpdating = True
        
     
    End Sub
    In the example, I would want an email to be formulated for Fred Bloggs, then another for Joe Soap then another for "Another Person" through to end.

    Any help would be greatly appreciated.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,253

    Re: Repeat macro for next row

    Try:
    Sub Training_Enrolment_email()
        ' Working in Office 2000-2016
        Dim OutApp      As Object
        Dim OutMail     As Object
        Dim strbody     As String
    
        Set OutApp = CreateObject("Outlook.Application")
    
        Do Until IsEmpty(ActiveCell.Value)
            Set OutMail = OutApp.CreateItem(0)
    
            strbody = "..." <-- Insert relevant HTML code
    
            On Error Resume Next
    
            With OutMail
                .Display
                .SentOnBehalfOfName = "Name of mailbox"
                .To = ActiveCell
                .CC = ""
                .BCC = ""
                .Subject = "Mandatory Training"
                .HTMLBody = strbody & "<br>" & .HTMLBody
                .Display    '.Send will be used
    
            End With
            ActiveCell.Offset(1).Select
        Loop
    
        On Error GoTo 0
        Set OutMail = Nothing
        Set OutApp = Nothing
    
        'Turn back on screen updating
        Application.ScreenUpdating = True
    
    End Sub
    Artik

  3. #3
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,428

    Re: Repeat macro for next row

    Sub Training_Enrolment_email()
    Dim StrBody As String, cell As Range
    For Each cell In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
       StrBody = "Whatever"
       With CreateObject("Outlook.Application").CreateItem(0)
            .Display
            .SentOnBehalfOfName = "Name of mailbox"
            .To = cell.Offset(, 2)
            .Subject = "Mandatory Training"
            .HTMLBody = StrBody & "<br>" & .HTMLBody
            .Display
        End With
    Next cell
    End Sub
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,253

    Re: Repeat macro for next row

    sintek, I wonder if With CreateObject("Outlook.Application") shouldn't be before the For Each loop.

    Artik

  5. #5
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,428

    Re: Repeat macro for next row

    Quote Originally Posted by Artik View Post
    sintek, I wonder if With CreateObject("Outlook.Application") shouldn't be before the For Each loop.

    Artik
    Nope...Then the same Outlook window is edited...That way a new window is created during each loop...

  6. #6
    Forum Contributor
    Join Date
    03-08-2011
    Location
    London
    MS-Off Ver
    Work Office 365 Home 2019
    Posts
    428

    Re: Repeat macro for next row

    Artik and sintek,

    Thank you so much, both suggestions work and achieve what I asked (I'll rep you guys up shortly)

    I have to say there is absolutely NOTHING wrong with sintek's solution, but I'm going to go with Artik's. The reason being that, whether by accident or design, it includes an ability for me to select a certain cell within a row and the macro operates from this point.

    If for example I have an original list of 100 people and send the email to them. Then, at a later date, I add another 50 people (effectively rows 101 - 150), I can select the appropriate cell on row 101, run the macro and it will work from that row onward. This could prove to be invaluable.

    Thank you both again.

    Kindest Regards

    MagicMan

  7. #7
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,253

    Re: Repeat macro for next row

    MagicMan, you can also use the sintek code slightly modified
    Sub Training_Enrolment_email_Sintek2()
        Dim StrBody As String, cell As Range
    
        With CreateObject("Outlook.Application")
            
            For Each cell In Range("A" & ActiveCell.Row & ":A" & Cells(Rows.Count, 1).End(xlUp).Row)
    
                StrBody = "Whatever"
    
                With .CreateItem(0)
                    .Display
                    .SentOnBehalfOfName = "Name of mailbox"
                    .To = cell.Offset(, 2)
                    .Subject = "Mandatory Training"
                    .HTMLBody = StrBody & "<br>" & .HTMLBody
                    '.Display
                End With
                
            Next cell
            
        End With
    End Sub
    It will be even safer as you do not have to select the cell in column C. You can select in any other column. As important is that the active cell is not in a row below the data range.

    Artik

  8. #8
    Forum Expert Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,428

    Re: Repeat macro for next row

    @ Artik

    Now I see what you meant...Yes, much better than creating App instance al the time...

+ 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] macro row to repeat at top
    By max_max in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-07-2016, 03:36 PM
  2. [SOLVED] Repeat Macro on Each Row
    By krayon in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-23-2015, 01:55 AM
  3. Replies: 4
    Last Post: 03-08-2014, 08:01 AM
  4. [SOLVED] Macro - Repeat macro a specific number of times
    By uvinduds in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-25-2013, 02:42 PM
  5. Repeat macro
    By wrightie in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-14-2010, 05:34 AM
  6. How to repeat a macro
    By Leon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-03-2006, 09:00 AM

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