Written by Allen Wyatt (last updated January 21, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003
When you create macros in VBA, you can easily incorporate the use of message boxes. These are typically used to convey information to the user and to get some rudimentary input. You include message boxes by using the MsgBox command. The following portion of a macro creates a very simple message box:
MsgBox "The Macro is Done"
You can also add symbols to your message boxes by including one a symbol type code as part of your MsgBox invocation. These symbols are used extensively in many Windows dialog boxes. The following four types of symbols can be used:
Type | Symbol | |
---|---|---|
16 | Stop sign | |
32 | Question mark in a circle | |
48 | Exclamation point in a circle | |
64 | Information symbol (lowercase i in a circle) |
As an example, let's suppose you wanted to include the exclamation point symbol. This is typically included in dialog boxes as a notice of when something important has happened or is about to happen. To include this symbol in your message box, you would include the following macro code:
MsgBox "Can't run this macro on this text", 48
So far the MsgBox command has been used as a statement, but you can also use it as a function. If you do so, you can use it to get simple input from the user. To make the MsgBox function more useful, Excel allows you to display more clickable buttons in the dialog box besides the OK button. This is done by adjusting the type code, which was used for the symbols displayed in the message box. The following are the different button combinations you can display in your message box:
Type | Button Types | |
---|---|---|
1 | OK, Cancel | |
2 | Abort, Retry, Ignore | |
3 | Yes, No, Cancel | |
4 | Yes, No | |
5 | Retry, Cancel |
To use the buttons, you simply add the value of the button type to the value you want used for the symbol. In the previous example, you used the code of 48 to display the exclamation point symbol. If you wanted to also include the Abort, Retry, Ignore buttons, you simply change the code to 50, which is 48 (the symbol code) plus 2 (the button code).
When using buttons in this way, the MsgBox function returns a value indicating which button the user chose. The buttons return, from left to right, -1, 0, and 1. Thus, if you use a button code of 3, then -1 would mean the user chose Yes, 0 would mean No, and 1 would mean Cancel.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2265) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Macros can be used for all sorts of data processing needs. One need that is fairly common is the need to move data from ...
Discover MoreWhen you need to get rid of characters in the middle of a cell value, the best way to do it is through the use of macros. ...
Discover MoreExcel allows you to specify the RGB (red, green, and blue) value for any color used in a cell. Here's a quick way to see ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments