Hack 57 Get Simple User Input for a Macro![]() ![]() useful. This hack shows you how to get feedback from a user without the overhead or complexity of a VBA UserForm.When you get input from a user (often yourself), you may want to present the person with several choices. You can use a UserForm with radio buttons or checkboxes, but it might add unnecessary overhead and complexity. Instead, use the dialogs already built into VBA, such as the MsgBox and InputBox.For example, if you just need the user to select between two choicessay, "Red" or "Blue"you could display a standard MsgBox with its included "Yes" and "No" buttons. The following macro displays the dialog shown in Figure 7-1: Sub PickRedOrBluePlease( ) Figure 7-1. Hack VBA's built-in dialogs to get simple user input![]() titlebar. To offer the choice of canceling the dialog, use the vbYesNoCancel value for the Buttons parameter.But what if you need more than two or three choices? How about choosing a number between 1 and 7? Try an InputBox and validate the response: Sub ChooseAmongSeven( )If the user fails to enter a whole number between 1 and 7, the macro displays the dialog in Figure 7-2. Figure 7-2. Using VBA's built-in dialogs to provide interaction from a macro![]() 7.2.1 Hacking the HackIf the user does not enter a valid choice, you can continue to offer the dialog instead of exiting the macro. The following code uses a Do loop to repeatedly prompt the user for a valid choice: Sub PromptUntilValid( ) |