我的VBA脚本中有一个Yes/No MsgBox
,它返回一个问题供用户回答。每当"是"或"否"按下按钮,除了运行其各自代码的脚本之外,另一个MsgBox
按下数字" 6"或" 7"弹出。如何禁用此秒MsgBox
?
这是我的代码:
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
答案 0 :(得分:11)
MsgBox
函数返回vbMsgBoxResult
值,这是一个枚举(应该是Long
整数,而不是Integer
。)
你打电话两次:
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
一次分配question
,一次显示question
- 此时将包含vbYes
(6)或vbNo
(7)。< / p>
我会声明question As vbMsgBoxResult
以避免歧义,并在您以后使用它时获得自动完成/智能感知。实际上,result
或answer
将是更好的标识符 - “问题”听起来像问题本身,而不是用户的回复。
答案 1 :(得分:8)
只需使用
question = "Unfortunately, the Database has no sources regarding " & Category & " in " & country & ". Would you like to broaden your search and see all sources regarding " & country & "?."
就够了。
此外,if函数可以是
If Msgbox(Question) = vbYes then
...
End If
不要两次调用MsgBox
答案 2 :(得分:1)
删除MsgBox question
。这会不必要地创建第二个填充了问题值的消息框(6或7,具体取决于您是选择是或否,例如vbYes的返回值为6)。