MsgBox是/否Excel VBA [英] MsgBox Yes/No Excel VBA

查看:72
本文介绍了MsgBox是/否Excel VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 MsgBox with the numbers "6" or "7" pops up. How do I disable this second 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

推荐答案

MsgBox 函数返回一个 vbMsgBoxResult 值,该值是一个枚举(应为 Long 整数,而不是 Integer ).

The MsgBox function returns a vbMsgBoxResult value, which is an enum (and should be a Long integer, not an 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).

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

我会将 question声明为vbMsgBoxResult ,以避免歧义并在以后使用它时获得autocomplete/IntelliSense.实际上, result answer 会是更好的标识符-"question"听起来像问题本身,而不是用户的响应.

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

这篇关于MsgBox是/否Excel VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆