Using Msg Box with Yes No and Cancel buttons

A number of other button combinations can be used with MsgBox. These include displaying Yes, No and Cancel buttons. The following VBA statement will implement this:

answer = MsgBox (Title:="Confirm Choice ", Prompt: ="Do you want to save this file?", Buttons: = 3)

The effect of this statement will produce the output shown below:

Do you want to save this FileV ~ Yes No Cancel

Figure 3.9 The MsgBox function with Yes /No and Cancel buttons In the example shown in Figure3.9, Title, Prompt, and Buttons have been assigned values. The Title and Prompt text values are, as expected, displayed in the appropriate MsgBox positions. The Buttons assignment takes any value in the range 1 to 6 and generates a different combination of buttons depending on the value chosen (see Table 3.1).

Table 3.1 MsgBox buttons options

Value

Response Buttons displayed

1

OK

Cancel

2

Abort

Retry

Ignore

3

Yes

No

Cancel

4

Yes

No

5

Retry

Cancel

6

OK

Table 3.2 MsgBox return values

Value Returned

Constant

Description

1

vbOK

The OK button has been pressed

2

vbCancel

The Cancel button has been pressed

3

vbAbort

The Abort button has been pressed

4

vbRetry

The Retry button has been pressed

5

vbIgnore

The Ignore button has been pressed

6

vbYes

The Yes button has been pressed

7

vbNo

The No button has been pressed

0 0

Post a comment