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:
<The 6 and 7 are hard coded values that hold a special meaning. The 'MsgBox("Click Yes...")' call will return a number that will let your code determine what the user did with the message box and you can then use conditionals (your IF statements) to decide what to do next.
A full list of these special values can found in the MSDN documentation here:
http://msdn.microsoft.com/en-us/library/139z2azd(VS.80).aspx
MsgBox
does return an Enum(eration)
called MsgBoxResult
, which is basically nothing else then numeric values with a 'label'. 6 and 7 in this case are members of this enum, which are mapped to the answers Yes
and No
.
Using so called 'magic numbers' instead of Constants or Enums should avoided whenever possible.
Basically, you could rewrite the code to 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
Might be that the Enum is called vbMsgBoxResult or something... I don't have an Office to verify this, just Visual Studio.
While we are on it... this 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
Just rewrite to the equivalent:
Private Sub CommandButton1_Click()
Dim optionSelected As VbMsgBoxResult
optionSelected = MsgBox("Click Yes to Proceed, No to stop", vbYesNoCancel, "Login")
If optionSelected = vbYes Then
Range("A1").Value = "You may proceed"
ActiveWorkbook.Activate
ElseIf optionSelected = vbNo Then
ActiveWorkbook.Close
End If
End Sub
And move on
These are return value from MsgBox(). The author should have used their symbolic value instead to make the program more readable:
vbYes 6
vbNo 7
See this MSDN article for more info
When I first started with MsgBox
answers, I almost always declared the answer as an Integer
. However, I learned that the best thing to do is to declare your message
variable as VbMsgBoxResult
, which is an enumeration that will always show the available answers. In the picture below, the IDE (e.g. the Visual Basic for Application editor) will show you the possible options available in VbMsgBoxResult
.
You could store your answer
variable as an Integer
since all of the variables in the Enumeration (e.g. vbAbort
, vbYes
, vbOK
, etc.) do in fact resolve to integers. However, you have to figure out what the integer values for those variables are every time you want to reference them. In my opinion, it's a better practice to store your answer as VbMsgBoxResult
so you can actually see the available answers.
This link is for VBScript, but I think the return codes should be the same: MsgBox Function Reference
The Return Codes tell you which button was clicked:
1 OK
2 Cancel
3 Abort
4 Retry
5 Ignore
6 Yes
7 No