MsgBox buttons argument settings - reverse engineering.

HighAndWilder

Well-known Member
Joined
Nov 4, 2006
Messages
712
Office Version
  1. 365
Platform
  1. Windows
Hi

I would like to be able to use the standard MsgBox button argument settings to pass a value to a custom message box form that
I have developed.

For example :

vbYesNo : 4
vbRetryCancel : 5
vbCritical : 16

and to be able to combine them as required.

For example :

vbYesNo + vbCritical

This example passes a value of 20 which gets reversed engineered by Excel so that the MsgBox function can use the values of 4 and 16
to display the 'Yes' and 'No' buttons and the Critical icon.

Excel seems to display a level of intelligence and handles inappropriate combination of settings in way in which is obvious to the developer.

Does anybody know of a way to create an array of the individual argument values or the approach that I could take in
developing my own?

Cheers

HighAndWilder
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
The basic technique is to use numbers that are powers of 2 as your base numbers.

vbYesNo = 4, vbOKOnly = 0, vbOKCancel = 1, vbCritical = 16 vbInformation = 64 and your argument is BoxStyle

So you code would look like

Code:
butYes.Visible = CBool(BoxStyle And 4)
butNo.Visible = CBool(BoxStyle And 4)
butCancel.Visible = CBool(BoxStyle And 1)

If CBool(BoxStyle And 16) Then
    Me.Caption = "Critical"
ElseIf CBool(BoxStyle and 64) Then
    Me.Caption = "Information"
End If
Using the arithmetic And operator allows you to add arguments to combine their effect.

Consider that arrays of boolean values
{True, False, True, True, False, False, False}
Are the same as strings TFTTFFF
Are the same as other strings 1011000
Are the same as binary representations of numbers
are the same as numbers 88

NOTE binary numerals are read right to left, but arrays are read left to right, this can cause confusion
I prefer the system where Array(True, False, True, True, False, False, False) is equivalent to 0001101 (reversing the order) is equivalent to 13 (8+4+1). But you gotta keep your head straight whatever system you prefer.
 
Last edited:
Upvote 0
Assuming your users will specify the sum of the same arguments as are available for a normal MessageBox, here are some functions that you may find useful. Except for the last one, they all return a text string indicating which option has been specified... change them to text strings that you like better or change them to some coded number system of your own choosing (remember to change the function type declaration from String to the appropriate number type if you do do this).
Code:
Function WhichButton(ByVal MsgBoxArg As Long) As String
  Select Case CLng(MsgBoxArg) Mod 16
    Case 0: WhichButton = "OK"
    Case 1: WhichButton = "OK, Cancel"
    Case 2: WhichButton = "Abort, Retry, Ignore"
    Case 3: WhichButton = "Yes, No, Cancel"
    Case 4: WhichButton = "Yes, No"
    Case 5: WhichButton = "Retry, Cancel"
  End Select
End Function

Function WhichIcon(ByVal MsgBoxArg As Long) As String
  Select Case (CLng(MsgBoxArg) Mod 128) - (CLng(MsgBoxArg) Mod 16)
    Case 16: WhichIcon = "Critical"
    Case 32: WhichIcon = "Question mark"
    Case 48: WhichIcon = "Exclamation"
    Case 64: WhichIcon = "Information"
  End Select
End Function

Function WhichDefault(ByVal MsgBoxArg As Long) As String
  Select Case (CLng(MsgBoxArg) Mod 1024) - (CLng(MsgBoxArg) Mod 128)
    Case 0:   WhichDefault = "First button"
    Case 256: WhichDefault = "Second button"
    Case 512: WhichDefault = "Third button"
    Case 768: WhichDefault = "Fourth button"
  End Select
End Function

Function WhichModal(ByVal MsgBoxArg As Long) As String
  If CLng(MsgBoxArg) Mod 8192 > 4096 Then
    WhichModal = "System"
  Else
    WhichModal = "Application"
  End If
End Function

Function HasHelpButton(ByVal MsgBoxArg As Long) As Boolean
  HasHelpButton = CLng(MsgBoxArg) And 16384
End Function
 
Upvote 0
Thanks Rick.

That is perfect.

I have implemented it and it works fine.

HighAnd Wilder
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top