MsgBox Yes/No Excel VBA
MsgBox Yes/No Excel VBA
我的 VBA 脚本中有一个 Yes/No MsgBox
returns 问题供用户回答。每当按下 "Yes" 或 "No" 按钮时,除了脚本 运行 其各自的代码外,还会弹出另一个带有数字“6”或“7”的 MsgBox
。如何禁用第二个 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 question
。这不必要地创建了第二个消息框,其中填充了问题的值(6 或 7,具体取决于您选择是还是否,例如 vbYes 的 return 值为 6)。
MsgBox
函数 returns 一个 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) .
我会声明 question As vbMsgBoxResult
以避免歧义,并在您以后使用它时得到 autocomplete/IntelliSense。实际上,result
或 answer
会是更好的标识符 - "question" 听起来像是 问题本身 ,而不是用户的回答。
只需使用
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 两次
我的 VBA 脚本中有一个 Yes/No MsgBox
returns 问题供用户回答。每当按下 "Yes" 或 "No" 按钮时,除了脚本 运行 其各自的代码外,还会弹出另一个带有数字“6”或“7”的 MsgBox
。如何禁用第二个 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 question
。这不必要地创建了第二个消息框,其中填充了问题的值(6 或 7,具体取决于您选择是还是否,例如 vbYes 的 return 值为 6)。
MsgBox
函数 returns 一个 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) .
我会声明 question As vbMsgBoxResult
以避免歧义,并在您以后使用它时得到 autocomplete/IntelliSense。实际上,result
或 answer
会是更好的标识符 - "question" 听起来像是 问题本身 ,而不是用户的回答。
只需使用
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 两次