Excel VBA Range Input: How to Use Application.InputBox
In this article
To get a range input from a user in Excel VBA, use the Application.InputBox method with the Type:=8 argument. This specific configuration triggers a dialog box that allows the user to either type a cell reference or use their mouse to click and drag a selection directly on the spreadsheet, returning a Range object that your code can then manipulate.
What You'll Need
- Microsoft Excel installed on Windows or macOS.
- The Developer tab enabled in the Excel Ribbon.
- Basic familiarity with opening the Visual Basic Editor (VBE).
- A workbook with some sample data to test the range selection.
Step-by-Step
- Open the Visual Basic Editor: Press
Alt + F11on your keyboard to launch the VBE. If you are on a Mac, you may need to use the Developer tab and click the Visual Basic button. - Insert a New Module: In the Project Explorer window on the left, right-click your workbook name, select
Insert, and then clickModule. This provides a clean slate to write your procedure. - Declare a Range Variable: At the start of your subroutine, you must declare a variable to hold the user's selection. Use
Dim myRange As Range. This ensures that VBA treats the input as a group of cells rather than a simple piece of text. - Write the InputBox Code: Use the following syntax to prompt the user:
Set myRange = Application.InputBox("Please select a range", Type:=8). It is critical to use theSetkeyword because a Range is an object, not a value. TheType:=8tells Excel to specifically expect a range selection. - Implement Error Handling: If a user clicks "Cancel" on the input box, VBA will throw a runtime error because it cannot assign a "False" value to a Range object. To prevent this, wrap your input code in
On Error Resume Next. Immediately after the input line, check if the range variable is nothing usingIf myRange Is Nothing Then Exit Sub. Then, useOn Error GoTo 0to turn normal error reporting back on. - Perform an Action on the Range: Now that the range is stored in
myRange, you can apply logic to it. For example, usemyRange.Interior.Color = vbYellowto highlight the selected cells orMsgBox "You selected " & myRange.Addressto confirm the selection. - Run the Macro: Press
F5or click the Run button. When the prompt appears, use your mouse to highlight a block of cells and click OK.
Common Mistakes
One of the most frequent errors is forgetting the Set keyword. In VBA, standard variables (like Integers or Strings) are assigned using a simple equals sign, but Objects (like Worksheets or Ranges) require Set. If you write myRange = Application.InputBox(...), you will receive an "Object variable or With block variable not set" error.
Another common mistake is confusing InputBox (the basic VBA function) with Application.InputBox (the Excel-specific method). The basic InputBox function only returns a String. It cannot be used to select cells with a mouse. To get the range selection functionality, you must prefix it with Application. and include the Type:=8 parameter.
Users also often forget to handle the "Cancel" button. Because Application.InputBox returns a Boolean False when canceled, and you are trying to assign that result to a Range object, the code will crash instantly without a proper On Error Resume Next wrapper or a variant-based check.
Tips
To make your tool more professional, use the Default argument in the InputBox. For example, Application.InputBox("Select Range", Default:="A1:B10", Type:=8). This pre-fills the box with a suggestion, saving the user time if they usually select the same area.
If you want to ensure the user only selects a specific number of cells, you can add a validation check immediately after the input. Use If myRange.Cells.Count > 100 Then to warn the user if they have selected too large an area, and then use a loop or another InputBox to ask them to try again.
Combine range input with Application.Intersect. If your macro is only supposed to work within a specific table, you can check if the user's input overlaps with your table range. If Application.Intersect(myRange, TableRange) Is Nothing, you can alert the user that their selection is outside the valid data area.
Troubleshooting
The code crashes when I click Cancel: This happens because a Range variable cannot hold the value "False". Ensure you have On Error Resume Next placed exactly one line above the Set myRange = ... line. Then, check If myRange Is Nothing Then Exit Sub to gracefully end the macro.
The InputBox appears, but I can't select cells with my mouse: Double-check that you have included Type:=8. If you omit this, or set it to Type:=2 (text), Excel will treat the input as a string and will not allow the mouse-selection interface to activate.
I get a "Type Mismatch' error: This usually occurs if you declared myRange as a String instead of a Range. Verify your declaration at the top of the sub: it must be Dim myRange As Range.
The macro selects cells on the wrong sheet: Application.InputBox allows the user to click on other sheets while the box is open. If your subsequent code assumes the range is on the ActiveSheet, it may fail. To fix this, always reference the parent sheet of the range: use myRange.Worksheet.Name to identify where the selection actually occurred.
Frequently Asked Questions
Can I force the user to select only one cell?
You cannot force the selection within the dialog itself, but you can validate it immediately after. Check If myRange.Cells.Count > 1 Then and display a message box telling the user to select only a single cell before exiting the sub.
Does this work on Excel for Mac?
Yes, Application.InputBox with Type:=8 is supported on Excel for Mac, though the visual appearance of the dialog box may differ slightly from the Windows version.
How do I limit the input to a specific sheet?
The InputBox allows users to switch sheets. To prevent errors, you can check If myRange.Worksheet.Name <> "Sheet1" Then. If the name doesn't match your required sheet, you can notify the user and stop the process.
What is the difference between Type 8 and other types?
Type:=8 is specifically for Range objects. Other types include Type:=1 for numbers, Type:=2 for strings, and Type:=4 for Boolean values. Only Type 8 enables the mouse-selection feature on the grid.
Can I use this to select multiple non-contiguous ranges?
Yes, if the user holds the Ctrl key (or Cmd on Mac) while selecting, they can pick multiple separate areas. The myRange object will then contain multiple "Areas". You can iterate through them using For Each area In myRange.Areas.