简体   繁体   English

如果值为“ no”,则Excel VBA发送邮件

[英]Excel VBA send Mail if value is “no”

In my excel file i have a table which look like this: 在我的excel文件中,我有一个如下表:

|Name|   Mail Address   | Return|
|ABC | abc@example.com  |   no  |
|DEF | def@example.com  |  yes  |
|GHI | ghi@example.com  |   no  |

Now i want to send a E-Mail with VBA to every one in the Table which has the value "No" in column "Return" (C). 现在,我想向VB中的每个人发送一封电子邮件,该电子邮件在“返回”(C)列中的值为“否”。

My Code looks like this: 我的代码如下所示:

Private Sub CommandButton2_Click()

    Dim OutApp As Object
    Dim OutMail As Object
    Dim nameList As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error GoTo cleanup

    For i = 2 To 22
        If Sheets("SHEETNAME").Range("C2:C22").Value = "no" Then
            nameList = nameList & ";" & Sheets("SHEETNAME").Range("B" & i).Value
        End If
    Next
        .To = nameList
        .Subject = "Friendly Reminder"
        .Body = "Text"
    End With 

cleanup:
    Set OutApp = Nothing

End Sub

The Code sends the mail, but it sends it to all people in the list. 代码发送邮件,但将其发送给列表中的所有人。 How can i solve this problem that only the people with the value "no" will receive the mail? 我该如何解决只有值“ no”的人才能收到邮件的问题?

Thanks for your help 谢谢你的帮助

Inside your For i = 2 To 22 loop you need to use the i , which you use as Row inside your Range` object. For i = 2 To 22循环内,您需要使用i ,将其用作Range对象inside your Row。

Change: 更改:

If Sheets("SHEETNAME").Range("C2:C22").Value = "no" Then

to: 至:

If Sheets("SHEETNAME").Range("C" & i).Value = "no" Then

Note : in your code above you have End With without an opening With statement. 注意 :在上面的代码中,您有End With而没有一个With语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM