简体   繁体   English

Excel VBA:如何捕获 MsgBox 响应

[英]Excel VBA: How to capture MsgBox response

I would like to perform the following tasks:我想执行以下任务:

  • write the message "Are you awake?"写下信息“你醒了吗?” and display a question mark并显示一个问号

  • capture the response in the integer variable intR, put intR value in cell A2捕获整数变量 intR 中的响应,将 intR 值放入单元格 A2

  • If the response is Yes, then write in cell A1 "Hurray"如果回答为是,则在单元格 A1 中写下“Hurray”

  • If the response is No, then write a message box with the text "ZZZZZZZZ"如果响应为否,则写一个带有文本“ZZZZZZZZ”的消息框

  • If the response is Cancel, then exit the sub如果响应是取消,则退出子

    Sub EX3_1_6MsgBoxFunction() Dim intR As Integer Dim TxtRng As Range Dim stra As String Dim stra2 As String 'Have the message box display the buttons Yes, No and Cancel intR = MsgBox("Are you awake ? ", vbQuestion + vbYesNoCancel) intR = Range("a2") If intR = vbYes Then Range("a1") = "Hurray" 'that means yes ElseIf intR = vbNo Then stra2 = MsgBox("ZZZZZZZZZZ") Else Range("a2") = intR End If End Sub

You may try something like this.....你可以试试这样的......

Sub EX3_1_6MsgBoxFunction()
Dim Ans As VbMsgBoxResult

'Have the message box display the buttons Yes, No and Cancel
Ans = MsgBox("Are you awake ? ", vbQuestion + vbYesNoCancel)

Select Case Ans
    Case vbYes
        Range("A1").Value = "Hurray"
        Range("A2").Value = "Yes"
    Case vbNo
        Range("A1").Value = "ZZZZZZZZZZ"
        Range("A2").Value = "No"
    Case Else
        Range("A1").Value = ""
        Range("A2").Value = "Calcel"
End Select

End Sub

First of all welcome to StackOverflow.首先欢迎来到 StackOverflow。 When you ask a question, please remember to specify where you have problems in your code.当您提出问题时,请记住指出您的代码中存在问题的地方。

  • write the message "Are you awake?"写下信息“你醒了吗?” and display a question mark并显示一个问号

If I understand correctly, you don't just want a question mark to be displayed, but a choice of 3 possibilities, which are given by the following MsgBox's mode:如果我理解正确的话,您不只是想要显示一个问号,而是要选择 3 种可能性,这些可能性由以下 MsgBox 的模式给出:

    intR = MsgBox("Are you awake ? ", vbQuestion + vbYesNoCancel)

This does display a MsgBox which requires an answer : "Yes" or "No" or "Cancel".这确实会显示一个需要回答MsgBox :“是”或“否”或“取消”。 It cannot be anything else which is why I removed the last bullet point in your question.它不可能是其他任何东西,这就是为什么我删除了您问题中的最后一个要点。The answer code is then mapped on an integer intR , that is correct.然后将答案代码映射到整数intR ,这是正确的。

  • capture the response in the integer variable intR, put intR value in cell A2捕获整数变量 intR 中的响应,将 intR 值放入单元格 A2

As YowE3K pointed out, it should be Range("a2").Value = intR , not intR = Range("a2")正如 YowE3K 指出的那样,它应该是Range("a2").Value = intR ,而不是intR = Range("a2")

  • If the response is Yes, then write in cell A1 "Hurray"如果回答为是,则在单元格 A1 中写下“Hurray”
  • If the response is No, then write a message box with the text "ZZZZZZZZ"如果响应为否,则写一个带有文本“ZZZZZZZZ”的消息框
  • If the response is Cancel, then exit the sub如果响应是取消,则退出子

You can use a Select Case here: think about it as an If with more than 2 possibilities:您可以在此处使用Select Case :将其视为具有超过 2 种可能性的If

Select Case intR
    Case 6 '<- According to the link I provided vbYes = 6
        Range("a1") = "Hurray"
    Case 7 '<- According to the link I provided vbNo = 7
        MsgBox "ZZZZZZZZ"
    Case 2 '<- According to the link I provided vbCancel = 2
        Exit Sub
End Select

You can write vbYes, vbNo and vbCancel instead of the integer.您可以编写 vbYes、vbNo 和 vbCancel 而不是整数。 As pointed out by another user, the code is probably more readable that way.正如另一位用户所指出的那样,这样的代码可能更具可读性。

Further to Noldor's answer, as a general rule, you shouldn't use Hungarian notation for your variable names, intR doesn't really tell you anything about what the variable is apart from it's type.除了 Noldor 的回答之外,作为一般规则,您不应该对变量名称使用匈牙利表示法, intR并没有真正告诉您变量除了它的类型之外是什么。

You should be able to infer the type from the context and the variable name should give you something more useful.您应该能够从上下文中推断出类型,并且变量名应该会给您一些更有用的信息。 I'd recommend rewriting your code like我建议像这样重写你的代码

Sub EX3_1_6MsgBoxFunction()
    Dim retVal As Integer

    'Ask the user whether they're awake
    retVal = MsgBox("Are you awake ? ", vbQuestion + vbYesNoCancel)

    'Switch on their answer
    Select Case retVal
        Case vbYes
            Range("A2") = "Hurray"
        Case vbNo
            MsgBox "ZZZZZZZ"
    End Select
End Sub

Note that you don't need to assign MsgBox to a variable, you can just call it with an argument (in this case "ZZZZZ" ) to prompt the user, this way you can eliminate your needless stra2 and stra .请注意,您不需要将 MsgBox 分配给变量,您只需使用参数(在本例中为"ZZZZZ" )调用它来提示用户,这样您就可以消除不必要的stra2stra You also don't need to explicitly Exit Sub because the code will naturally exit after the Select Case or If statements.您也不需要显式Exit Sub因为代码会在Select CaseIf语句之后自然退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM