lundi 16 avril 2018

Excel VBA send Mail if value is "no"

In my excel file i have a table which look like this:

|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).

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?

Thanks for your help

Aucun commentaire:

Enregistrer un commentaire