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: Using InputBox to Get Data.

Using InputBox to Get Data

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


3

If you are developing a simple custom application in Excel, you may want to use the InputBox function to retrieve information from the user, and then place that information in a particular place in a worksheet. This can be easily done in the following manner:

UserValue = InputBox("Value to use?")
Cells(1, 1).Value = UserValue

These two lines, when inserted into a macro, prompt the user for input. This input is assigned to the UserValue variable by the InputBox function. The contents of this variable are then deposited in cell A1 of the current worksheet using the Cells method. If you prefer, you could also use the Range object to specify a location for the value, as shown here:

UserValue = InputBox("Value to use?")
Range("B3").Value = UserValue

This example deposits the value of UserValue into cell B3.

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 (12496) 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: Using InputBox to Get Data.

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

Finding Fields

Fields allow you to add simple dynamic content to your document. Here's how you can find the fields when you need to know ...

Discover More

Reordering the Display of a Data Series

Once you create a chart, you aren't limited to keeping the data series in the order they originally appeared. You can ...

Discover More

Formatting Issues with Indexing Levels

When you insert an index in a document, Word automatically takes care of formatting that index. What if the index levels ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Recording a Macro

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

Searching Very Large Strings in a Macro

VBA provides a few different ways you can search for information within strings. This tip looks at the most efficient ...

Discover More

Macro, while Running, Stops Excel from Responding

When running a macro, have you ever seen Excel appear to stop responding? This can be frustrating, but there are a couple ...

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 five less than 6?

2023-12-10 11:28:47

J. Woolley

Some more notes about VBA's InputBox and Excel's Application.InputBox:
VBA's InputBox function is modal. Prompt is limited to approximately 1024 characters. XPos/YPos are in twips; both are ignored if either is omitted. The default position is centered about one-third down the screen; changes to the default position are not persistent. A String is returned (max 254 characters); it is null ("") if the user clicks Cancel.
Excel's Application.InputBox method is non-modal, so the user can select worksheet cells or copy/paste from another application. Prompt is limited to 255 characters. Left/Top are in points but do not seem to have any effect; changes to the default position are persistent (apparently saved in the Registry). If return Type is specified, it will validate the input and permit a correction; default Type is String. False is returned if the user clicks Cancel. If Type is 8, a Range object is returned; in this case, On Error code is necessary. For example,
    Dim R as Range
    Set R = Nothing
    On Error Resume Next
    Set R = Application.InputBox(..., Type:=8)
    On Error GoTo 0
    If R Is Nothing Then ... 'user clicked Cancel
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 function only permits the latter. Otherwise, these custom functions work the same as the standard ones.
See https://sites.google.com/view/MyExcelToolbox/


2023-12-10 03:06:06

Alex Blakenburg

It is worth being aware that you also have the option of using Application.InputBox which allows you to specify the type of data to be entered and in doing so provides some automated validation. Also if the type of data is Cell reference it allows the user to use the mouse to select the Cells or Cells.


2023-12-09 12:38:00

J. Woolley

Obviously, the Tip's code can be simplified. For example:
    Cells(1, 1).Value = InputBox("Value to use?")
It should be noted that VBA's InputBox function returns a String (text). If the String represents a numeric, date, or logical value, Excel converts it accordingly when it is "deposited" in cell A1.
For more on this subject, see https://excelribbon.tips.net/T011416


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.