【问题标题】:Send email using column values as conditions使用列值作为条件发送电子邮件
【发布时间】:2021-02-03 14:53:38
【问题描述】:

我需要将工作表每一行的内容邮寄到“U”列中的电子邮件地址。程序需要验证“需要发送”(V 列)以及是否已经“通过电子邮件发送”(W 列)。

有一个类似的问题,但对我没有帮助。

一些细节:

  • 电子邮件地址列(U 列)是一个公式,它使用另一个工作表中的 VLOOKUP 公式搜索电子邮件。
  • 宏运行(无调试),但没有任何反应。未创建邮件草稿。
  • 我尝试只使用一个条件,它会为整个列表创建所有草稿(似乎工作正常)
Sub email()
    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("query_export_results").Columns("U").Cells.SpecialCells(xlCellTypeFormulas)
        Set OutMail = OutApp.CreateItem(0)
        If cell.Value Like "?*@?*.?*" And _
            LCase(Cells(cell.Row, "V").Value) = "Y" And _
            LCase(Cells(cell.Row, "W").Value) = "" Then
            With OutMail
                .To = Cells(cell.Row, "U").Value
                .Subject = "Notificação de Não Conformidade | Non-Compliance Notification"
                .Body = "Notificação de Registro de Não Conformidade."
                .display
                '.Send
            End With
            Cells(cell.Row, "W").Value = "sent"
            Set OutMail = Nothing
        End If
    Next cell
    
    'Set OutApp = Nothing
    Application.ScreenUpdating = True
End Sub

【问题讨论】:

  • 您能否更清楚地解释“我尝试仅使用一个条件,它会为整个列表创建所有草稿(似乎工作正常)”是什么意思?什么是“一个条件”?
  • 您好,先生!谢谢回答。我的意思是在“If cell.Value Like”?*@?*.?*“And _”这一行上,如果我删除“And _”并且不使用其他两个后续行,它就可以工作。但为工作表的所有行创建草稿。
  • 我认为问题出在LCase(Cells(cell.Row, "W").Value) = "" 当单元格为空时,excel 无法将其转换为小写。试试Cells(cell.Row, "W")=""

标签: excel vba email conditional-statements


【解决方案1】:

您的条件LCase(Cells(cell.Row, "V").Value) = "Y" 永远不会为真,因为您有一个大写“Y”,但您正在测试一个小写 单元格值。将LCase 更改为UCase 或将“Y”更改为“y”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 2016-04-17
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    相关资源
    最近更新 更多