Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. 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.

Getting User Input in a Dialog Box

Written by Allen Wyatt (last updated May 14, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


2

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.)

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11416) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Getting User Input in a Dialog Box.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Summing Every Fourth Cell in a Row

Need to sum a series of cells that fits some regular pattern? Here are several ways that you can get the summation that ...

Discover More

Changing the Way Footnotes Are Numbered

Most footnotes in a document start numbering with the number 1 and proceed from there through the rest of your document. ...

Discover More

ExcelTips: Filters and Filtering

Excel provides two ways to filter your data so that only what you want to see is displayed. Discover how filtering works ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Understanding Macros

What is a macro? Ever wonder what these are and how to use them? This tip answers the basics of what a macro is used for, ...

Discover More

Copying Named Ranges

Named ranges are a great tool to use in developing formula-heavy workbooks. You may want, at some point, to copy your ...

Discover More

Disabled Macros

Do your macros seem to be disabled on your new machine? It could be because of the security settings in Excel. Here's ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2022-05-14 10:34:47

J. Woolley

My Excel Toolbox includes the InputBoxVBA_Custom and InputBoxApp_Custom functions. Both allow positioning that can be either the location of a worksheet's cell or absolute screen coordinates. The standard VBA InputBox and Excel Application.InputBox functions only permit the latter.
See https://sites.google.com/view/MyExcelToolbox
and https://excelribbon.tips.net/T012496_Using_InputBox_to_Get_Data.html


2022-05-14 05:14:01

Kiwerry

Thanks, Allen.
It seemed worthwhile to mention briefly that Excel VBA has two Input boxes: the function described above, and a similar method, "Application.InputBox". The latter gives the programmer more control over the type of input required of the user.
See https://docs.microsoft.com/en-us/office/vba/api/excel.application.inputbox If you're interested


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.