Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Getting User Input in a Dialog Box.
by Allen Wyatt
(last updated July 9, 2016)
If you need to get input from a user under control of a macro, one method you can use is to employ the InputBox function. This function displays a dialog box and allows the user to type a response. The result is a string, returned to your macro, which you can then process and use.
The syntax for the InputBox function is as follows:
sResponse = InputBox(sPrompt, sTitle, sDefault)
There are three parameters you can use with InputBox (each of them strings), although only the first one is absolutely required. In this syntax, sPrompt is the text you want displayed as the user prompt, sTitle is the text to display in the title bar of the dialog box, and sDefault is the default text string offered to the user in the dialog box. The user can edit or accept the default string, as desired.
As an example, the following code lines can be used to display a dialog box and ask the user for his or her name:
Dim sUserName as String Dim sPrompt as String Dim sTitle as String Dim sDefault as String sPrompt = "Please check your name and make any corrections" sTitle = "Name Entry" sDefault = "John Doe" sUserName = InputBox(sPrompt, sTitle, sDefault)
When this code is completed, the sUserName variable contains whatever the user entered. You can then, in your macro, use that input in any other way that you see fit.
The InputBox function is great for grabbing one piece of data at a time, as you can only post a single "question" using it. If you need to get a bunch of user input for use in your macro, then you'll need to rely either on a UserForm or grab the input directly from a worksheet. (Both approaches have been discussed in other ExcelTips.)
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11416) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Getting User Input in a Dialog Box.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
One of the most common ways of creating macros is to use Excel's macro recorder. This tip shows how easy it is to use the ...Discover More
Import a bunch of ZIP Codes into Excel, and you may be surprised that any leading zeroes disappear. Here's a handy little ...Discover More
You can use macros to make your common Excel tasks easier and faster. For instance, if you routinely need to create new ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.