了解从 MsgBox 返回的响应代码

发布于 2024-08-11 20:08:06 字数 664 浏览 4 评论 0原文

我对编程很陌生,我刚刚开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

欢你一世 2024-08-18 20:08:06

MsgBox 确实返回一个名为 MsgBoxResultEnum(eration),它基本上只是带有“标签”的数值。本例中的 6 和 7 是该枚举的成员,它们映射到答案 YesNo

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

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

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

可能 Enum 被称为 vbMsgBoxResult 或其他...我没有 Office 来验证这一点,只有 Visual Studio。

当我们讨论这个问题时......这可能更容易理解:

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

MsgBox does 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 Yes and No.

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.

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
背叛残局 2024-08-18 20:08:06

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

VbMsgBoxResult

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

When I first started with MsgBox answers, I almost always declared the answer as an Integer. However, I learned that the best thing to do is to declare your message variable 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.

VbMsgBoxResult

You could store your answer variable as an Integer 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 as VbMsgBoxResult so you can actually see the available answers.

梦里梦着梦中梦 2024-08-18 20:08:06

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

返回代码告诉您哪个按钮被点击:

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

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:

1   OK
2   Cancel
3   Abort
4   Retry
5   Ignore
6   Yes
7   No
|煩躁 2024-08-18 20:08:06

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

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

代码应该说

If message = Constants.vbYes 

而不是

If message = 6

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

If message = Constants.vbYes 

instead of

If message = 6

So that it is clear what is happening.

烈酒灼喉 2024-08-18 20:08:06

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

vbYes   6
vbNo    7

请参阅此 MSDN 文章 了解更多信息

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

vbYes   6
vbNo    7

See this MSDN article for more info

没企图 2024-08-18 20:08:06

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

最好的做法是不要在代码中直接使用这些数字,而是使用 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

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:

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
醉酒的小男人 2024-08-18 20:08:06

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

携君以终年 2024-08-18 20:08:06

只需重写为等效内容:

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

然后继续

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文