MsgBox returns an Enum(eration) called MsgBoxResult , which basically is nothing more than numeric values with a label. 6 and 7 in this case are members of this enumeration, which are compared with the answers Yes and No
The use of so-called “magic numbers” instead of constants or Enums should be avoided whenever possible.
Basically, you can rewrite the code for this:
Dim message As Integer message = MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login") If message = MsgBoxResult.Yes Then Range("A1").Value = "You may proceed" ActiveWorkbook.Activate ElseIf message = MsgBoxResult.No Then ActiveWorkbook.Close End If
It is possible that Enum is called vbMsgBoxResult or something else ... I do not have Office to test this, just Visual Studio.
While we're on it ... it might be easier to understand:
Select Case MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login") Case MsgBoxResult.Yes Range("A1").Value = "You may proceed" ActiveWorkbook.Activate Case MsgBoxResult.No ActiveWorkbook.Close Case MsgBoxResult.Cancel ' he clicked cancel ' End Select
Bobby Nov 18 '09 at 2:30 p.m. 2009-11-18 14:30
source share