Using the InputBox function
Input boxes are used to capture information from users. By its very nature, it obtains only one piece of information at a time, be it a value, date, or text information. It is the easiest, and also the most common way of gathering information from users.
Like message boxes, we can create and customize our own input boxes in VBA.
In this recipe, we will be using the InputBox
function to create and manipulate input boxes to suit our needs.
Getting ready
Make sure that DialogBoxes.xlsm
is still open. Sheet1
should be active. Press Alt + F11 to switch to the VBA Editor, then insert a new module.
How to do it…
Enter the following code to create an input box:
- In Module2, create the following Sub procedure:
Sub AskForName() InputBox "Please enter your name" End Sub
- Press F5 to run the code. The following input box will appear. Click on the Cancel button to close the dialog box: