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.
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
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:
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.
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!
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 MoreExcel allows you to add pictures to your worksheet, even within a macro. However, you might have a bit harder time ...
Discover MoreAs 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 MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments