Microsoft Office Tutorials and References
In Depth Information
Emulating the MsgBox Function
MsgBox emulation: MyMsgBox code
The MyMsgBox function uses a UserForm named MyMsgBoxForm . The function itself, which
follows, is very short. The bulk of the work is done in the UserForm_Initialize procedure.
The complete code for the MyMsgBox function is too lengthy to list here, but it’s
available in a workbook named msgbox emulation.xlsm , available on the companion
CD-ROM. The workbook is set up so that you can easily try various options.
Public Prompt1 As String
Public Buttons1 As Integer
Public Title1 As String
Public UserClick As Integer
Function MyMsgBox(ByVal Prompt As String, _
Optional ByVal Buttons As Integer, _
Optional ByVal Title As String) As Integer
Prompt1 = Prompt
Buttons1 = Buttons
Title1 = Title
MyMsgBox = UserClick
Figure 15-11 shows MyMsgBox in use. It looks very similar to the VBA message box, but I used a
different font for the message text (Calibri 12-point bold) and also used some different icons.
Figure 15-11: The result of the MsgBox emulation function.
If you use a multiple monitor system, the position of the displayed UserForm may not be
centered in Excel’s window. To solve that problem, use the following code to display the
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)