0

解决了!

我必须验证某些单元格不是空的,所以我想创建一个子程序并传递我需要检查的变量。

这就是我想出的:

Sub errorMessage(errMsg As String, errRange As String)
If Range(errRange) = "" Then
    MsgBox errMsg, , "Error:"
    Range(errRange).Activate
    'this is what i was looking for :doh:, the 'end' line terminates everything..
    END
End Sub

现在,当我从按钮调用它时,它实际上会结束按钮的子部分吗?

IE

Private Sub CommandButton1_Click()

    Call errorMessage("name is missing", "D4")

    'this function shouldn't be called if there was a msgbox displayed with the above call 
sendEmail 


End Sub

我怎样才能做到这一点?

编辑:

好的所以这就是我解决它的方式,我试图这样做的原因是为了避免buttonClick sub中的大量代码行,你的想法是什么?

请记住,在执行 sendEmail 子之前,这个东西必须检查大约 25 个问题的空白。

Private Sub CommandButton1_Click()


    Call validateEntry("Client Name is missing.", "D4")
    Call validateEntry("# not complete.", "D5")


    Call validateEntry("Address same as CV?", "D6")


    Call validateEntry("Number missing.", "D8")
    Call validateEntry("Type missing.", "D9")
    Call validateEntry("Q1 requires a Yes or No.", "E19")
    Call validateEntry("Q2 requires a Yes or No.", "E21")
    Call validateEntry("Q3 requires a Yes or No.", "E23")
    Call validateEntry("Q4 requires a Yes or No.", "E25")
    Call validateEntry("Q5 requires a Date.", "D28")
    Call validateEntry("Q6 requires a Yes or No.", "E30")
    Call validateEntry("Q7 requires a Yes or No.", "E32")



MsgBox "passed"
'sendEmail
End Sub


Sub validateEntry(errMsg As String, errRange As String)
    If Range(errRange) = "" Then
        MsgBox errMsg, , "Error:"
        Range(errRange).Activate
        End
    End If
End Sub
4

2 回答 2

2

So, in your example, you're looking for the "passed" notification to only be sent when there is data in cell D4, right?

This should work:

Private Function errorMessage(errMsg As String, errRange As String) As Boolean
    errorMessage = False

    If Len(Trim(Range(errRange))) = 0 Then
        MsgBox errMsg, , "Error:"
        Range(errRange).Activate

        errorMessage = True
    End If
End Function

Public Sub CommandButton1_Click()
    If errorMessage("name is missing", "D4") = False Then
        MsgBox "passed"
    End If
End Sub

Alternatively, you can handle all MsgBox notifications from within the function, to group similar logic together, and keep the Button Click Event Sub clean:

Private Function errorMessage(errMsg As String, errRange As String)
    If Len(Trim(Range(errRange))) = 0 Then
        MsgBox errMsg, , "Error:"
        Range(errRange).Activate
    Else
        MsgBox "passed"
    End If
End Function

Public Sub CommandButton1_Click()
    Call errorMessage("name is missing", "D4")
End Sub
于 2013-02-07T21:26:21.213 回答
1

这里有很多误解。

首先,不,默认情况下它不会结束按钮例程。你需要在你的按钮中处理它。

接下来,您End If在这里缺少一个地方:

Sub errorMessage(errMsg As String, errRange As String)
    If Range(errRange) = "" Then  ' This may not be the best way to check for 
                                  ' an empty range
        MsgBox errMsg, , "Error:"
        Range(errRange).Activate
        Exit Sub
End Sub

你真的不想要一个子程序,你想要一个返回布尔值的函数,像这样:

Function errorMessage(errMsg As String, errRange As String) as Boolean
    ' Function returns True if an error occured
    errorMessage = False
    If Range(errRange) = "" Then
        MsgBox errMsg, , "Error:"
        Range(errRange).Activate
        errorMessage = True
    End If
End Sub

然后在这里:

Private Sub CommandButton1_Click()

    If errorMessage("name is missing", "D4") Then
        Exit Sub
    End If

    'this msgbox should not display if the above msgbox is displayed
    MsgBox "passed"

    ' continue on with all of your fun processing here
End Sub
于 2013-02-07T21:27:39.447 回答