Excel VBA: Select Range Based on Cell Value
In this article
You select a range based on a cell value in Excel VBA by reading the value into a variable, then using that variable to build a range reference with Range, Cells, Offset, or Find. The shortest working pattern is: Dim v As String: v = Range("A1").Value: Range(v).Select. That works when the cell holds a valid address such as B2:D10. For most real tasks you will instead use the value to calculate a row or column number, or to locate a matching cell with Find, then select from there. The steps below cover both approaches and the mistakes that trip people up.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A workbook saved as
.xlsmif you want to keep your macro. - Basic familiarity with the VBA editor (Alt+F11 on Windows, Option+F11 on Mac).
- A sheet with a cell that holds either a range address, a row number, or a search value you want to match.
Menu labels and dialog wording vary slightly between Excel versions and between Windows and Mac, but the VBA objects and methods used here are the same on both platforms.
Step-by-Step
- Open the VBA editor and insert a module. Press Alt+F11 (Option+F11 on Mac), then in the Project pane right-click your workbook and choose Insert > Module. Code in a standard module can be run with Alt+F8.
- Decide what the cell value represents. There are three common cases: the cell contains a literal address like
B2:D10; the cell contains a row or column number; or the cell contains text you want to find somewhere else on the sheet. The code you write depends on which one you have. - Read the value into a variable. Use a typed variable so errors surface early:
For a numeric row, useDim addr As String addr = Range("A1").ValueDim r As Long: r = Range("A1").Value. Reading into a variable first makes the next steps readable and lets you validate the value before using it. - Case 1 — the cell holds a range address. Build the range directly:
If the address might include a sheet name, useRange(addr).SelectRange(addr).Selectonly when the sheet is already active; otherwise qualify it, for exampleWorksheets("Sheet1").Range(addr).Select. Note thatSelectrequires the sheet to be active, so activate it first if needed. - Case 2 — the cell holds a row number. Convert the number to a row and select a fixed number of columns:
This selects columns A through E on that row. Change the column numbers to suit your layout.Dim r As Long r = Range("A1").Value Range(Cells(r, 1), Cells(r, 5)).Select - Case 3 — the cell holds text to find. Use
Findto locate the first matching cell, then expand the selection:Dim found As Range Set found = Cells.Find(What:=Range("A1").Value, LookIn:=xlValues, LookAt:=xlWhole) If Not found Is Nothing Then found.Select End IfLookAt:=xlWholematches the entire cell; usexlPartfor partial matches. Always test forNothingbefore selecting, or you will get a run-time error 91 when no match exists. - Extend the selection if needed. Once you have a starting cell, use
ResizeorOffsetto select a block. For example,found.Resize(10, 3).Selectselects a 10-row by 3-column block starting at the found cell.found.Offset(1, 0).Selectmoves one row down. - Avoid
Selectwhere you can. Selecting is slow and fragile. If you only need to read or write values, act on the range object directly:found.Resize(10, 3).Value = "x". UseSelectonly when the user needs to see the range highlighted or when you are recording a macro for personal use. - Run and test. Press F5 in the editor or Alt+F8 from Excel. Test with an empty cell, a wrong address, and a value that does not exist, so you see how the code behaves before relying on it.
Common Mistakes
- Not checking for
NothingafterFind. This is the most common cause of error 91. Always wrap the result in anIf Not found Is Nothingtest. - Selecting a range on an inactive sheet.
Range("A1").Selectfails if the sheet is not active. Activate the sheet first, or work with the range object without selecting it. - Passing a number where an address is expected.
Range(5)means row 5, not cell A5. If your cell holds a row number, useRows(5)orCells(5, 1)to be explicit. - Forgetting that
Findremembers settings.Findkeeps the last usedLookIn,LookAt, andSearchOrderarguments if you omit them. Always specify them explicitly. - Using
ActiveCellorSelectionas the search base when you meant the whole sheet. SearchCellsor a specific range instead. - Leaving the macro in a normal module when it needs to react to changes. Event code such as
Worksheet_Changemust sit in the sheet module, not a standard module.
Tips
- Use
Application.Gotoinstead ofSelectwhen you want to scroll the sheet to a range without changing the selection:Application.Goto found, True. - Trim and validate the cell value before using it:
addr = Trim(CStr(Range("A1").Value)). This avoids errors from stray spaces. - Store the target sheet in a variable (
Dim ws As Worksheet: Set ws = Worksheets("Data")) so you can qualify every range reference and avoid acting on the wrong sheet. - Turn off
ScreenUpdatingwhile looping over many cells, then turn it back on at the end; this makes macros noticeably faster. - If the cell value is a named range, use
Range(addr).Selectonly after confirming the name exists withThisWorkbook.Names(addr).
Troubleshooting
Error 1004 “Select method of Range class failed”. The sheet holding the range is not active, or the address is invalid. Activate the sheet, or drop Select and act on the range object directly.
Error 91 “Object variable or With block variable not set”. Find returned Nothing. Check the search value, the LookIn setting, and whether the value exists on that sheet.
Error 13 “Type mismatch”. You assigned text to a numeric variable. Use IsNumeric to test the cell value before converting, or declare the variable as Variant.
The macro selects the wrong range. Print the value you read with Debug.Print addr and check it in the Immediate window (Ctrl+G). The cell may contain hidden spaces or a formula result you did not expect.
Nothing happens when you run it. Confirm the code is in a standard module and that you are running the correct macro from Alt+F8. If the workbook is not saved as .xlsm, the macro will be lost on close.
It works once, then fails. Find settings persist between calls. Reset them by specifying every argument on each call, or by using Application.FindFormat.Clear if you use formatting-based searches.
Frequently Asked Questions
Can I select a range using a cell value without Select?
Yes. Build the range object and act on it directly, for example Range(addr).Interior.Color = vbYellow. This is faster and does not depend on which sheet is active.
How do I select a range based on a cell value on another sheet?
Qualify the range with the sheet: Worksheets("Data").Range(Worksheets("Input").Range("A1").Value).Select. Activate the target sheet first if you need the selection to be visible.
Why does Range("A1").Value return a date or number instead of the text I see?
The cell holds a real date or number, not text. Use .Text to get the displayed string, or convert with CStr and Format to control the output.
Can I select multiple non-contiguous ranges from a cell value?
Yes, if the value is a valid multi-area address such as A1:A5,C1:C5. Range(addr).Select handles it. Building such addresses programmatically with Union is more reliable than string concatenation.
Does this work in Excel for Mac?
Yes. The VBA object model is the same. Only the keyboard shortcuts differ: Option+F11 opens the editor and Option+F8 opens the macro list.
How do I make the selection happen automatically when a cell changes?
Put a Worksheet_Change event in the sheet module and check Target.Address before running your selection code. Standard modules do not receive sheet events.