Understanding what response codes come back from MsgBox

前端 未结 8 1739
春和景丽
春和景丽 2021-02-19 08:32

I\'m very new to programming and I\'m just starting to learn VBA with excel. I came across on this website and did the examples here but I have question about this code:

<
8条回答
  •  无人共我
    2021-02-19 09:36

    6 and 7 are the return codes from the MsgBox method. Basically, when MsgBox is called, it shows a message-box to the user, who clicks either "Yes", "No", or "Cancel". The user's selection is returned from the MsgBox method as a number, where 6 is Yes, and 7 is No.

    It is considered best-practice not to use these numbers in your code directly, but instead to use Microsoft supplied constants which represent them. Your code could be re-written as:

    Private Sub CommandButton1_Click()
        Dim message As Integer
        message = MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login")
        If message = vbYes Then
            Range("A1").Value = "You may proceed"
            ActiveWorkbook.Activate 
        ElseIf message = vbNo Then
            ActiveWorkbook.Close
        ElseIf message = vbCancel Then
            'Do nothing.
        End If
    End Sub
    

提交回复
热议问题