在 Excel 中结束宏

Ending macro in Excel

因此,我从用户 Vityata 的 Ron de Bruin 那里得到了这段代码的帮助,但是我无法让宏停止 运行ning 一旦它 运行s 离开 WO 和电子邮件。如果我在 .send 之后放置“停止”,我必须一遍又一遍地单击 运行,直到所有电子邮件都已发送并且所有内容都标记为 'sent',然后在最后一个上它不会停止运行宁直到我逃跑。我想找到一种方法让代码停止 运行ning 一旦没有更多的工作订单(与尚未发送的电子邮件配对)留给电子邮件发送。如果有办法在 2018 年工作表的一栏中也记录阅读回执,那将非常有帮助,但我一直在努力。我习惯于在 VBA 中创建表单,因此外出的信息对我来说一直很难自动化。

原文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

编辑: 我尝试使用 Do Loop 函数,但没有成功

问题是您 运行 遍历了 T 列中的所有单元格,因为范围 Worksheets("2018").Columns("T").Cells 包含完整的列。

在您的子

开头添加以下代码
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

并将 for 循环更改为

For Each cell In rg

rg 仅包含 T 列的填充单元格。这样,代码仅 运行s 通过包含数据的单元格。

PS 根据评论中的信息,您需要像这样对您的条件进行编码

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