如何编写如果范围内的单元格具有EMAIL值,然后向这些值发送电子邮件

时间:2019-07-03 16:05:06

标签: excel vba

我有3列(“ O”,“ Q”,“ S”),它们使用VLOOKUP从输入的人名中获取电子邮件地址。我希望所有这些人都收到一封包含一些详细信息的电子邮件。如果3列中没有任何电子邮件值,则我不希望运行此代码。如果三列中的任何一列中都包含电子邮件地址,我都希望运行该代码...

我有这段代码可以检查一个单元格是否包含电子邮件地址,然后向该电子邮件地址发送电子邮件,但是我需要所有3个单元格都使用此代码,并且我需要将所有这些单元格一起通过电子邮件发送。

If emailbox.Value = True Then

On Error Resume Next
i = ActiveCell.Row                  'VLOOKUP
Sheet2.Cells(i, 15).Value = Application.WorksheetFunction.VLookup(Sheet2.Cells(i, 14).Value, Sheet3.Range("AMS"), 2, 0)

On Error Resume Next
i = ActiveCell.Row                  'VLOOKUP
Sheet2.Cells(i, 17).Value = Application.WorksheetFunction.VLookup(Sheet2.Cells(i, 16).Value, Sheet3.Range("AQE"), 2, 0)


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

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

    On Error GoTo cleanup

        For Each cell In Sheet2.Range("O" & ActiveCell.Row)
        If cell.Value Like "?*@?*.?*" Then 'Check cell for email address

            Set OutMail = OutApp.CreateItem(0)

            With OutMail
                .To = cell.Value
                .Subject = "SUBJECT"
                .Body = "Dear...."



                .Display
            End With
            On Error GoTo 0
            Set OutMail = Nothing

            sent.Visible = True
            Else
        End If
   Next cell

cleanup:
    Set OutApp = Nothing
    Application.ScreenUpdating = True
End If

1 个答案:

答案 0 :(得分:0)

遍历column O中的每个单元格,并测试所有三个单元格是否有电子邮件,当遇到电子邮件时,它将执行其余代码,并退出内部循环。

Dim cel As Range, lRow As Long, x As Long
lRow = ActiveSheet.Cells(Rows.Count, 15).End(xlUp).Row

    For Each cel In Range("O2:O" & lRow)
        For x = 15 To 19 Step 2 'loop through each cell in the active row

            If Cells(cel.Row, x).Value Like "?*@?*.?*" Then
                'email .To = Cells(cel.Row, x).Value
                Exit For 'exits the inner loop when the condition is met
            End If

        Next x

    Next cel