MsgBox 是/否 Excel VBA

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

MsgBox Yes/No Excel VBA

excelvbaexcel-vbamsgbox

提问by franciscofcosta

I have a Yes/No MsgBoxin my VBA script that returns a question for the user to answer. Whenever the "Yes" or "No" buttons are pressed, besides the script running its respective code, another MsgBoxwith the numbers "6" or "7" pops up. How do I disable this second MsgBox?

Yes/No MsgBox我的 VBA 脚本中有一个返回问题供用户回答。每当按下“是”或“否”按钮时,除了运行其各自代码的脚本外,MsgBox还会弹出另一个带有数字“6”或“7”的脚本。如何禁用这一秒MsgBox

Here's my code:

这是我的代码:

Dim question As Integer
question = MsgBox("Unfortunately, the Database has no sources regarding " & Category & " in " & country & ". Would you like to broaden your search and see all sources regarding " & country & "?", vbYesNo + vbQuestion, "Empty Sheet")

MsgBox question

If question = vbYes Then
    Sheets("Results").Range("D6").ClearContents
    Sheets("Results").Range("D7").ClearContents
    Category = Sheets("Results").Range("D6").Value
Else
    Sheets("Results").Range("D5").ClearContents
    Sheets("Results").Range("D6").ClearContents
    Sheets("Results").Range("D7").ClearContents
    Exit Sub
End If

回答by Mathieu Guindon

The MsgBoxfunction returns a vbMsgBoxResultvalue, which is an enum (and should be a Longinteger, not an Integer).

MsgBox函数返回一个vbMsgBoxResult值,它是一个枚举(并且应该是一个Long整数,而不是一个Integer)。

You're calling it twice:

你调用它两次:

Dim question As Integer
question = MsgBox("Unfortunately, the Database has no sources regarding " & Category & " in " & country & ". Would you like to broaden your search and see all sources regarding " & country & "?", vbYesNo + vbQuestion, "Empty Sheet")

MsgBox question
Dim question As Integer
question = MsgBox("Unfortunately, the Database has no sources regarding " & Category & " in " & country & ". Would you like to broaden your search and see all sources regarding " & country & "?", vbYesNo + vbQuestion, "Empty Sheet")

MsgBox question

Once to assign question, and once to display question- which at that point is going to contain either vbYes(6) or vbNo(7).

一次分配question,一次显示question- 此时将包含vbYes(6) 或vbNo(7)。

enter image description here

在此处输入图片说明

I would declare question As vbMsgBoxResultto avoid ambiguities and get autocomplete/IntelliSense when you later use it. Actually, resultor answerwould be a better identifier - "question" sounds like the question itself, not the user's response.

我会声明question As vbMsgBoxResult避免歧义并在您以后使用它时获得自动完成/智能感知。实际上,result或者answer将是一个更好的标识符 - “问题”听起来像是问题本身,而不是用户的回答。

回答by Rosetta

just use

只是使用

question = "Unfortunately, the Database has no sources regarding " & Category & " in " & country & ". Would you like to broaden your search and see all sources regarding " & country & "?."

would be enough.

就足够了。

In addition the if function can be

另外 if 函数可以是

If Msgbox(Question) = vbYes then 
    ...
End If

Don't call MsgBox twice

不要两次调用 MsgBox

回答by bobajob

Remove MsgBox question. This is unnecessarily creating a second message box populated with the value of question (6 or 7 depending on whether you chose yes or no, as eg vbYes has the return value 6).

删除MsgBox question。这不必要地创建了第二个消息框,其中填充了问题的值(6 或 7,取决于您选择是还是否,例如 vbYes 的返回值是 6)。