vba 了解从 MsgBox 返回的响应代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1756513/
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-08 10:08:41  来源:igfitidea点击:

Understanding what response codes come back from MsgBox

vbamsgbox

提问by tintincutes

I'm very new to programming and I'm just starting to learn VBA with excel. I came across on this website and did the examples here but I have question about this code:

我对编程很陌生,我刚刚开始用 excel 学习 VBA。我在这个网站上遇到并在这里做了例子,但我对这段代码有疑问:

I know the variables are declared using "Dim" statement "Message" here is the variable with a data type of integer. What I don't clearly understand is; what is the meaning of "6" here and "7". I believe they come from somewhere. But as I just started learning this program, I don't have any idea. Could you please tell me how it end up to "6" and "7". I believe there is some basis here

我知道变量是使用“Dim”语句声明的,“Message”这里是数据类型为整数的变量。我不明白的是; 这里的“6”和“7”是什么意思。我相信他们来自某个地方。但是当我刚开始学习这个程序时,我没有任何想法。你能告诉我它是如何变成“6”和“7”的。我相信这里有一些基础

Private Sub CommandButton1_Click()
Dim message As Integer
message = MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login")
If message = 6 Then
Range("A1").Value = "You may proceed"
ActiveWorkbook.Activate 
ElseIf message = 7 Then
ActiveWorkbook.Close
End If

End Sub

Thank you for your help:-)

感谢您的帮助:-)

=======

========

Thanks guys for the answers, they're very helpful. Yes this thread has been already posted in superuser site. I was informed that this question should belong here so I posted it here after reading that they will do it automatically from superuser to stackoverflow.

谢谢大家的回答,他们很有帮助。是的,此线程已发布在超级用户站点中。我被告知这个问题应该属于这里,所以我在阅读他们会自动从超级用户到 stackoverflow 后将它发布在这里。

thanks once again

再次感谢

回答by Bobby

MsgBoxdoes return an Enum(eration)called MsgBoxResult, which is basically nothing else then numeric values with a 'label'. 6 and 7 in this case are members of this enum, which are mapped to the answers Yesand No.

MsgBox确实返回了一个Enum(eration)被调用的MsgBoxResult,它基本上是带有“标签”的数值。在这种情况下,6 和 7 是该枚举的成员,它们被映射到答案YesNo

Using so called 'magic numbers' instead of Constants or Enums should avoided whenever possible.

应尽可能避免使用所谓的“幻数”而不是常量或枚举。

Basically, you could rewrite the code to this:

基本上,您可以将代码重写为:

Dim message As Integer
message = MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login")
If message = MsgBoxResult.Yes Then
    Range("A1").Value = "You may proceed"
    ActiveWorkbook.Activate
ElseIf message = MsgBoxResult.No Then
    ActiveWorkbook.Close
End If

Might be that the Enum is called vbMsgBoxResult or something... I don't have an Office to verify this, just Visual Studio.

可能是 Enum 被称为 vbMsgBoxResult 或什么......我没有办公室来验证这一点,只有 Visual Studio。

While we are on it... this might be easier to understand:

当我们在做的时候……这可能更容易理解:

Select Case MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login")
    Case MsgBoxResult.Yes
        Range("A1").Value = "You may proceed"
        ActiveWorkbook.Activate

    Case MsgBoxResult.No
        ActiveWorkbook.Close

    Case MsgBoxResult.Cancel
        ' he clicked cancel '

End Select

回答by Ben McCormack

When I first started with MsgBoxanswers, I almost always declared the answer as an Integer. However, I learned that the best thing to do is to declare your messagevariable as VbMsgBoxResult, which is an enumeration that will always show the available answers. In the picture below, the IDE (e.g. the Visual Basic for Application editor) will show you the possible options available in VbMsgBoxResult.

当我第一次开始MsgBox回答时,我几乎总是将答案声明为Integer. 但是,我了解到最好的做法是将message变量声明为VbMsgBoxResult,这是一个始终显示可用答案的枚举。在下图中,IDE(例如 Visual Basic for Application 编辑器)将向您显示VbMsgBoxResult.

VbMsgBoxResult

VbMsgBoxResult

You couldstore your answervariable as an Integersince all of the variables in the Enumeration (e.g. vbAbort, vbYes, vbOK, etc.) do in fact resolve to integers. However, you have to figure out what the integer values for those variables are every time you want to reference them. In my opinion, it's a better practice to store your answer as VbMsgBoxResultso you can actually see the available answers.

可以你的存储answer变量作为Integer,因为所有的枚举变量(例如vbAbortvbYesvbOK等)做实际上决心整数。但是,每次要引用它们时,您都必须弄清楚这些变量的整数值是什么。在我看来,存储您的答案是一种更好的做法,VbMsgBoxResult这样您就可以实际看到可用的答案。

回答by Brian Schroth

It's very poorly written code, "6" and "7" are the values of the constants "vbYes" and "vbNo" where are returned when the user clicks Yes or No on the dialog.

这是非常糟糕的代码,“6”和“7”是常量“vbYes”和“vbNo”的值,当用户在对话框上单击“是”或“否”时返回这些值。

Reference: http://www.techonthenet.com/access/constants/msgbox_ret.php

参考:http: //www.techonthenet.com/access/constants/msgbox_ret.php

The code should say

代码应该说

If message = Constants.vbYes 

instead of

代替

If message = 6

So that it is clear what is happening.

这样就很清楚发生了什么。

回答by Patonza

This link is for VBScript, but I think the return codes should be the same: MsgBox Function Reference

此链接适用于 VBScript,但我认为返回代码应该相同: MsgBox 函数参考

The Return Codes tell you which button was clicked:

返回代码告诉您单击了哪个按钮:

1   OK
2   Cancel
3   Abort
4   Retry
5   Ignore
6   Yes
7   No

回答by mjv

These are return value from MsgBox(). The author should have used their symbolic value instead to make the program more readable:

这些是 MsgBox() 的返回值。作者应该使用它们的符号值来使程序更具可读性:

vbYes   6
vbNo    7

See this MSDN articlefor more info

有关更多信息,请参阅此MSDN 文章

回答by RB.

6 and 7 are the return codes from the MsgBox method. Basically, when MsgBox is called, it shows a message-box to the user, who clicks either "Yes", "No", or "Cancel". The user's selection is returned from the MsgBox method as a number, where 6 is Yes, and 7 is No.

6 和 7 是 MsgBox 方法的返回码。基本上,当 MsgBox 被调用时,它会向用户显示一个消息框,用户单击“是”、“否”或“取消”。用户的选择作为数字从 MsgBox 方法返回,其中 6 是 Yes,7 是 No。

It is considered best-practice not to use these numbers in your code directly, but instead to use Microsoft supplied constants which represent them. Your code could be re-written as:

最好不要在代码中直接使用这些数字,而是使用 Microsoft 提供的常量来表示它们。您的代码可以重写为:

Private Sub CommandButton1_Click()
    Dim message As Integer
    message = MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login")
    If message = vbYes Then
        Range("A1").Value = "You may proceed"
        ActiveWorkbook.Activate 
    ElseIf message = vbNo Then
        ActiveWorkbook.Close
    ElseIf message = vbCancel Then
        'Do nothing.
    End If
End Sub

回答by Jesse Taber

The 6 and 7 are hard coded values that hold a special meaning. The 'MsgBox("Click Yes...")' call will return a number that will let your code determine what the user did with the message box and you can then use conditionals (your IF statements) to decide what to do next.

6 和 7 是具有特殊含义的硬编码值。'MsgBox("Click Yes...")' 调用将返回一个数字,让您的代码确定用户对消息框做了什么,然后您可以使用条件(您的 IF 语句)来决定下一步要做什么。

A full list of these special values can found in the MSDN documentation here:

这些特殊值的完整列表可以在 MSDN 文档中找到:

http://msdn.microsoft.com/en-us/library/139z2azd(VS.80).aspx

http://msdn.microsoft.com/en-us/library/139z2azd(VS.80).aspx

回答by eglasius

Just rewrite to the equivalent:

只需重写为等价物:

Private Sub CommandButton1_Click()
  Dim optionSelected As VbMsgBoxResult
  optionSelected = MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login")
  If optionSelected = vbYes Then
    Range("A1").Value = "You may proceed"
    ActiveWorkbook.Activate 
  ElseIf optionSelected = vbNo Then
    ActiveWorkbook.Close
  End If
End Sub

And move on

继续前进