Search code examples
excelvbaoutlookms-project

Ending macro in Excel


So, I was helped with this code from Ron de Bruin from user Vityata but I am having trouble getting the macro to STOP running once it runs out of WO's and emails. If I put ' stop ' in after .send I have to click run over and over until all the emails are sent and everything is marked as 'sent', and then on the last one it won't stop running until I hit escape. I want to find a way to make the code stop running once there are no more work orders (paired with emails that haven't been sent yet) left to email out. If there is a way to also note the read receipt in a column of the 2018 worksheet that would be extremely helpful but I've been struggling. I am used to creating forms in VBA, so information going OUT has always been difficult for me to automate.

The original post is here Original post

Sub test2()

Dim OutApp As Object
Dim OutMail As Object
Dim cell As Range

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")

For Each cell In Worksheets("2018").Columns("T").Cells
    Set OutMail = OutApp.CreateItem(0)
    If cell.Value Like "?*@?*.?*" Then      'try with less conditions first
        With OutMail
            .To = Cells(cell.Row, "T").Value
            .Subject = "Work Order: " & Cells(cell.Row, "G").Value & " assigned"
            .Body = "Work Order: " & Cells(cell.Row, "G").Value & _
                " has been assigned to you." & _
                vbNewLine & vbNewLine & _
                "Region: " & Cells(cell.Row, "B").Value & vbNewLine & _
                "District: " & Cells(cell.Row, "C").Value & vbNewLine & _
                "City: " & Cells(cell.Row, "D").Value & vbNewLine & _
                "Atlas: " & Cells(cell.Row, "E").Value & vbNewLine & _
                "Notification Number: " & Cells(cell.Row, "F").Value & vbNewLine
            .ReadReceiptRequested = True
            .OriginatorDeliveryReportRequested = True
            .Send
        End With
        Cells(cell.Row, "V").Value = "sent"
        Set OutMail = Nothing
    End If
    Next cell

'Set OutApp = Nothing                   'it will be Nothing after End Sub
 Application.ScreenUpdating = True

 End Sub

EDIT: I tried to use the Do Loop function with no luck


Solution

  • The issue is that you run through all cells in column T, because the range Worksheets("2018").Columns("T").Cells contains the complete column.

    Add the following code at the beginning of your sub

    Dim lastRow As Long
    Dim ws As Worksheet
    
    
        Dim rg As Range
        Set ws = Worksheets("2018")
    
        With ws
            lastRow = .Cells(Rows.Count, "T").End(xlUp).Row
            Set rg = Range(.Cells(1, "T"), .Cells(lastRow, "T"))
        End With
    

    And change the for loop to

    For Each cell In rg
    

    rg only contains the filled cells of column T. In this way the code only runs through the cells which contain data.

    PS Based on the information in the comment you would need to code your condition like that

    If cell.Value Like "?*@?*.?*" And UCASE(cell.Offset(0, 1).Value) <> "SENT" Then