Excel VBA如果Else语句停止代码

时间:2018-04-17 05:05:57

标签: excel vba excel-vba if-statement

在我的Excel文件中,我有一个CommandButton,如果细胞E1,K2和K5被填满,它会发送一封电子邮件。

不幸的是,我的代码并没有真正起作用。

Private Sub CommandButton1_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

    If Sheets("Sheetname").Range("E1").Value = "" Or _
    Sheets("Sheetname").Range("K2").Value = "" Or _
    Sheets("Sheetname").Range("K5").Value = "" Then
        MsgBox "The Cells E1, K2 and K5 has to be filled!"

    Else
        MsgBox "All Cells are filled, send E-Mail now"
        For i = 2 To 22
        If Sheets("efforts and risks").Range("BB2").Value <> "" Then
            nameList = nameList & ";" & Sheets("efforts and risks").Range("BB" & i).Value
        End If
    Next
        With OutMail
            .To = nameList
            .Subject = "subject line"
            .Body = "Body Text"
            .Send
        End With
    End If
cleanup:
    Set OutApp = Nothing
End Sub

如果单元格已填充,Excel将显示MsgBox,但不会发送邮件。我需要改变哪部分代码?

希望有人可以帮助我。非常感谢

0 个答案:

没有答案