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

Stopping Enter from being Pressed In a Form

If you create a form using Word, chances are good that you don't want a user to mess up the layout of the form by ...

Discover More

Document Shows as 'In Use' by Another User

Word tries to constantly track who is using various documents, in order to prevent two users from clashing in their edits ...

Discover More

Accessing Footnote Text in a Macro

Using macros you can access just about anything in a Word document. Accessing the text within footnotes is no exception, ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Calculating an ISBN Check Digit

ISBNs are used to uniquely identify books. You may need to know if an ISBN is valid or not, which involves calculating ...

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

Removing a Macro from a Shortcut Key

When you assign a macro to a shortcut key, you make it easy to run the macro without ever removing your hands from the ...

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 four minus 0?

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.