13

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:

I know the variables are declared using "Dim" statement "Message" here is the variable with a data type of integer. What I don't clearly understand is; what is the meaning of "6" here and "7". I believe they come from somewhere. But as I just started learning this program, I don't have any idea. Could you please tell me how it end up to "6" and "7". I believe there is some basis here

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

End Sub

Thank you for your help:-)

=======

Thanks guys for the answers, they're very helpful. Yes this thread has been already posted in superuser site. I was informed that this question should belong here so I posted it here after reading that they will do it automatically from superuser to stackoverflow.

thanks once again

Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61
tintincutes
  • 5,618
  • 25
  • 67
  • 86
  • From the FAQ (http://superuser.com/faq): "If your question is about programming, ask on stackoverflow.com". Your question will most likely be migrated to this site, you should create an account there and associate it to this one, so you keep ownership of this question. – Gnoupi Nov 18 '09 at 14:41
  • @Gnoupi: ok I'll post this question to Stackoverflow, although I already know the answer not really sure if that helps. – tintincutes Nov 18 '09 at 14:45
  • 1
    Don't post your question there, there is a system of migration between the sites, your question will be migrated there (as soon as enough persons vote for it, or a moderator does it). If you post it there, it will most likely be a duplicate, when this one will be moved (I know, it's not very clear at the beginning, but you get used to it ;) ) – Gnoupi Nov 18 '09 at 14:51
  • @Gnoupi: sorry about that i already post it there. I read the comments quite late. I hope I'll not get any penalty:-( – tintincutes Nov 18 '09 at 14:52
  • Although, if you have your answer here, don't hesitate to click to "tick" next to the correct answer. – Gnoupi Nov 18 '09 at 14:52
  • Don't worry, no penalty or anything, it's only about the uses of these sites. What will happen is that one question will be closed, with a link to the duplicate one, once there, simply. No reason to worry ;) – Gnoupi Nov 18 '09 at 14:54

8 Answers8

24

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
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Bobby
  • 11,419
  • 5
  • 44
  • 69
  • Good answer, I like suggesting referencing the enum value names instead of the numbers. Also +1 for probably being better suited for StackOverflow. – Jesse Taber Nov 18 '09 at 14:32
  • @appakz, thanks. I think using the names instead of the numbers is what enums are all about, and I just love enums. ;) – Bobby Nov 18 '09 at 14:37
  • thanks very much Bobby for the very good answer especially if this question belongs to Stackoverflow:-) I'm not sure if I have to open this to Stackoverflow though or just leave it here like this. +1 :-) – tintincutes Nov 18 '09 at 14:40
  • @Bobby: the first code doesn't work. i got an error "Run-Time Error "424" Object required"... not really sure why? – tintincutes Nov 18 '09 at 15:38
  • @Bobby: also the last code with the select case it doesn't work. I got the same error as above. – tintincutes Nov 18 '09 at 15:42
  • @tintincute: at which line does the debugger stop? – Bobby Nov 19 '09 at 09:22
  • @Bobby: I'm not really sure which line. I clicked the Command Button and then after that I select either yes, no or cancel then this microsoft visual basic will come out Run Time error '424' object required. with the button end debug... where can i see this line, the one you mean? – tintincutes Nov 19 '09 at 12:50
  • @Bobby: when I clicked on "Debug" it highlighted the line which says: "If message= MsgboxResult.Yes Then" – tintincutes Nov 25 '09 at 08:05
  • @tintincute: Oh...I'm sorry, try replacing `MsgBoxResult.Yes` with `vbYes` – Bobby Nov 25 '09 at 08:18
  • @Bobby: yes I tried that but it is still the same error. And if I clicked on "Debug" I get the same line highlighted. – tintincutes Nov 25 '09 at 14:23
10

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.

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.

Community
  • 1
  • 1
Ben McCormack
  • 32,086
  • 48
  • 148
  • 223
7

It's very poorly written code, "6" and "7" are the values of the constants "vbYes" and "vbNo" where are returned when the user clicks Yes or No on the dialog.

Reference: http://www.techonthenet.com/access/constants/msgbox_ret.php

The code should say

If message = Constants.vbYes 

instead of

If message = 6

So that it is clear what is happening.

Brian Schroth
  • 2,447
  • 1
  • 15
  • 26
  • thanks for comment. i'm not the one who did this code, I'm trying to understand it and it's my first time to learn this vba thing – tintincutes Nov 19 '09 at 08:32
7

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
Patonza
  • 6,557
  • 5
  • 25
  • 20
5

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

mjv
  • 73,152
  • 14
  • 113
  • 156
5

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
RB.
  • 36,301
  • 12
  • 91
  • 131
  • @RB: you're right, it is considered best-practice not to use these values especially for beginners like me. the first time I saw the code, I was really wondering where the h*** this 6 and 7 came from. As an engineer, I usually make some assumptions what could be, where could be these things coming from. That's what I bet, that they must be a constant values in VBA. Now I don't understand why they still develop these constant values if it is not a good practice to use them because at the end this might confuse the beginners?Does that mean that these constant values are only for experts? – tintincutes Nov 19 '09 at 08:49
  • The constants (vbNo, vbYes, etc) should always be used instead of the equivalent integers (6, 7, etc). Code which uses the integers directly is bad because it is extremely hard to read. Developers should not use the integers directly, but should use the equivalent constant. Hope that answers your question. – RB. Nov 19 '09 at 10:34
  • @RB: True I agree with you. Developers should not use the integers directly but use the equivalent constants. Why did they still develop this integers as equivalent constants. Sometimes it makes the program more complicated. – tintincutes Nov 19 '09 at 11:59
5

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

Jesse Taber
  • 2,376
  • 3
  • 23
  • 33
  • thanks for the helpful links. so the "6" and "7" are already fix values. I didn't know that there are enumeration values in Visual Basics. This is very helpful thanks again – tintincutes Nov 18 '09 at 14:38
3

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

eglasius
  • 35,831
  • 5
  • 65
  • 110