了解从 MsgBox 返回的响应代码
我对编程很陌生,我刚刚开始学习 VBA 和 Excel。我在这个网站上遇到并做了这里的示例,但我对这段代码有疑问:
我知道变量是使用“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
谢谢你的帮助:-)
=======
谢谢大家的回答,他们非常有帮助。是的,该主题已发布在超级用户站点中。我被告知这个问题应该属于这里,所以我在读到他们会从超级用户到 stackoverflow 自动执行此操作后将其发布在这里。
再次感谢
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:
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
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.
thanks once again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
MsgBox
确实返回一个名为MsgBoxResult
的Enum(eration)
,它基本上只是带有“标签”的数值。本例中的 6 和 7 是该枚举的成员,它们映射到答案Yes
和No
。应尽可能避免使用所谓的“幻数”而不是常量或枚举。
基本上,您可以将代码重写为:
可能 Enum 被称为 vbMsgBoxResult 或其他...我没有 Office 来验证这一点,只有 Visual Studio。
当我们讨论这个问题时......这可能更容易理解:
MsgBox
does return anEnum(eration)
calledMsgBoxResult
, 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 answersYes
andNo
.Using so called 'magic numbers' instead of Constants or Enums should avoided whenever possible.
Basically, you could rewrite the code to this:
Might be that the Enum is called vbMsgBoxResult or something... I don't have an Office to verify this, just Visual Studio.
While we are on it... this might be easier to understand:
当我第一次开始使用
MsgBox
答案时,我几乎总是将答案声明为Integer
。但是,我了解到最好的做法是将您的message
变量声明为VbMsgBoxResult
,这是一个始终显示可用答案的枚举。在下图中,IDE(例如 Visual Basic for Application 编辑器)将向您显示VbMsgBoxResult
中可用的选项。您可以存储您的
答案
变量作为Integer
,因为枚举中的所有变量(例如vbAbort
、vbYes
、vbOK
等) )实际上解析为整数。但是,每次要引用这些变量时,您都必须弄清楚这些变量的整数值是什么。在我看来,将答案存储为VbMsgBoxResult
是更好的做法,这样您就可以实际看到可用的答案。When I first started with
MsgBox
answers, I almost always declared the answer as anInteger
. However, I learned that the best thing to do is to declare yourmessage
variable asVbMsgBoxResult
, 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 inVbMsgBoxResult
.You could store your
answer
variable as anInteger
since 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 asVbMsgBoxResult
so you can actually see the available answers.此链接适用于 VBScript,但我认为返回代码应该相同:
MsgBox 函数参考
返回代码告诉您哪个按钮被点击:
This link is for VBScript, but I think the return codes should be the same:
MsgBox Function Reference
The Return Codes tell you which button was clicked:
这是写得非常糟糕的代码,“6”和“7”是当用户在对话框上单击“是”或“否”时返回的常量“vbYes”和“vbNo”的值。
参考:http://www.techonthenet.com/access/constants/msgbox_ret.php
代码应该说
而不是
So 清楚发生了什么。
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.
Reference: http://www.techonthenet.com/access/constants/msgbox_ret.php
The code should say
instead of
So that it is clear what is happening.
这些是 MsgBox() 的返回值。作者应该使用它们的符号值来使程序更具可读性:
请参阅此 MSDN 文章 了解更多信息
These are return value from MsgBox(). The author should have used their symbolic value instead to make the program more readable:
See this MSDN article for more info
6 和 7 是 MsgBox 方法的返回代码。基本上,当调用 MsgBox 时,它会向用户显示一个消息框,用户可以单击“是”、“否”或“取消”。用户的选择以数字形式从 MsgBox 方法返回,其中 6 表示“是”,7 表示“否”。
最好的做法是不要在代码中直接使用这些数字,而是使用 Microsoft 提供的代表它们的常量。您的代码可以重写为:
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.
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:
6 和 7 是具有特殊含义的硬编码值。 “MsgBox("Click Yes...")”调用将返回一个数字,让您的代码确定用户对消息框执行的操作,然后您可以使用条件(您的 IF 语句)来决定下一步要做什么。
这些特殊值的完整列表可以在 MSDN 文档中找到:
http://msdn.microsoft.com/en-us/library/139z2azd(VS.80).aspx
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.
A full list of these special values can found in the MSDN documentation here:
http://msdn.microsoft.com/en-us/library/139z2azd(VS.80).aspx
只需重写为等效内容:
然后继续
Just rewrite to the equivalent:
And move on