VBA:带有if语句并返回true或false的子程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14761134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:32:10  来源:igfitidea点击:

VBA: subroutine with if statement and returning true or false?

excelvbaexcel-vbasubroutine

提问by maxgohan

SOLVED!

解决了!

I have to validate that certain cells are not empty, so I want to create a subroutine and pass the variables I need checked.

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

This is what I came up with:

这就是我想出的:

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

Now when I call it from my button, will it actuall end the sub of the button?

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

i.e.

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

How can i make this happen?

我怎样才能做到这一点?

EDIT:

编辑:

OK So this is how i sovled it, the reason i'm trying to do this is to avoid tons of lines of code in the buttonClick sub, what are your thoughts??

好的所以这就是我如何解决它,我尝试这样做的原因是为了避免 buttonClick 子中的大量代码行,你有什么想法?

keep in mind that this thing has to check about 25 questions for blanks before executing the sendEmail sub....

请记住,在执行 sendEmail sub 之前,这件事必须检查大约 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

回答by armstrhb

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

因此,在您的示例中,您正在寻找仅在单元格 D4 中有数据时发送的“已通过”通知,对吗?

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:

或者,您可以从函数内部处理所有 MsgBox 通知,将类似的逻辑组合在一起,并保持按钮单击事件子干净:

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

回答by LittleBobbyTables - Au Revtheitroad

There are a number of misconceptions here.

这里有很多误解。

First, no, it will not end the button routine by default. You will need to handle that within your button.

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

Next, you're missing an End Ifsomewhere in here:

接下来,您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

You really don't even want a subroutine in the first place, you want a function that returns a boolean, like this:

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

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

And then here:

然后在这里:

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