Use Row Variables in an Excel VBA Range
Question details
Use VBA variable values to construct a row range and unhide those rows in Excel.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A macro needs to unhide rows from a starting row through the last used row in column B on the Kitchen worksheet.
- Observed behavior
- Variable names placed directly inside a quoted range string are treated as literal text rather than evaluated as row numbers.
Confirm that the workbook contains a worksheet named Kitchen and that column B identifies the last row you want to include. Open the VBA editor with Alt+F11 and locate the macro you want to update.
Concatenate Long variables to build the row range
Use this approach to unhide rows between a starting row and the last used row in column B.
Concatenate the variable values with a quoted colon to produce a range string such as 2:25. Declare row variables as Long because Excel row numbers can exceed the Integer limit of 32,767.
In Excel, press Alt+F11, then open the module containing your macro in the VBA editor.
Replace the macro with the following code, or adapt it to your existing procedure: Sub unhideAll() Dim ws As Worksheet Dim lr As Long, a As Long Set ws = ThisWorkbook.Worksheets("Kitchen") a = 2 lr = ws.Cells(ws.Rows.Count, 2).End(xlUp).Row If lr >= a Then ws.Rows(a & ":" & lr).Hidden = False End If End Sub
Click inside the procedure and press F5 in the VBA editor. Excel will unhide the specified rows on the Kitchen worksheet.

Work with spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface for opening and editing spreadsheets. Its Microsoft Office format compatibility helps you move existing files into WPS; check VBA support and test macros before relying on them.

Frequently Asked Questions
Why does Rows("a:lr") not use my variable values?
Everything inside the quotes is literal text. Use Rows(a & ":" & lr) to join the values of a and lr with a colon.
Why should I use Long instead of Integer for row numbers?
A VBA Integer can hold values only up to 32,767, while an Excel worksheet can have more rows. Declare row variables with Dim a As Long and Dim lr As Long.
How can I find the last used row on the Kitchen sheet?
Set ws to ThisWorkbook.Worksheets("Kitchen"), then use ws.Cells(ws.Rows.Count, 2).End(xlUp).Row to find the last used row in column B on that sheet.
Will the macro unhide rows if column B has no data below row 1?
No. The example checks If lr >= a before unhiding rows. With a set to 2, it makes no change when the last used row in column B is row 1.




