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

Positive and Negative Colors in a Chart

When creating a line cart, the line can show values both positive and negative values. This tip explains how you can use ...

Discover More

Quick AutoFill Variations

The AutoFill feature can be used for more than just incrementing information into cells. This tip explains how to access ...

Discover More

Shortcut for Em and En Spaces

Typographers know that not all spaces are created equal. When creating a document, most people use spaces created by ...

Discover More

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!

More ExcelTips (ribbon)

Easily Changing the Default Drive and Directory

Need a quick way to change the default drive and directory in a macro you are writing? Here's the commands to do it and a ...

Discover More

Removing Pictures for a Worksheet in VBA

Excel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...

Discover More

Inserting a Page Break Every X Rows

As you format your worksheet, Excel allows you to add page breaks where you'd like. If you want to put in a series 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 two more than 7?

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.