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

Searching for ASCII and ANSI Characters

Need to get down to the "character level" when searching for information in a document. Word allows you to search for any ...

Discover More

Dealing with Circular References

Circular references occur when a formula includes a reference to the cell in which the formula appears. Here's how you ...

Discover More

Preventing Automatic Date Formatting Changes

Excel often changes the formatting of a cell based on how it parses what you are entering into that cell. This is ...

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)

Creating an Animated Count Up

You might want to display a value in a cell as an upward-counting value. This might seem difficult but can be done rather ...

Discover More

Copying Worksheet Code Automatically

When creating a workbook to be used by others, you may want any worksheets they add to the workbook to contain some ...

Discover More

Macros Not Available in Later Versions of Excel

What would you do if you had a macro-enabled workbook that refused to work properly on computers using later versions of ...

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 2 + 2?

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.