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

Reducing the Curl in Printed Documents

Have you ever printed out a document, only to have the pages curl very badly as they come out of the printer? There's a ...

Discover More

Printing a Document's Mirror Image

If you need to print the mirror image (backwards) of a document, you may think you are out of luck in Word. There are ...

Discover More

Footnotes in Two Columns

When laying out how your printed pages will look, you might want to place your footnotes into more than one column. The ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

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

Swapping Two Strings

Strings are used quite frequently in macros. You may want to swap the contents of two string variables, and you can do so ...

Discover More

Making Worksheet Copies for Daily Shifts

Creating lot of copies of a worksheet is a snap within a macro. This tip introduces a macro that will make three copies ...

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 one less than 9?

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.