VBA & Macro Problems

Use Row Variables in an Excel VBA Range

Steven ParkerSteven Parker Dec 2, 2026 868 views

Question details

Use VBA variable values to construct a row range and unhide those rows in Excel.

Use Row Variables in an Excel VBA Range
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the macro

In Excel, press Alt+F11, then open the module containing your macro in the VBA editor.

2
Set the worksheet and row variables

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

3
Run the macro

Click inside the procedure and press F5 in the VBA editor. Excel will unhide the specified rows on the Kitchen worksheet.

Concatenate Long variables to build the row range
How the last row is determined: The code finds the last used cell in column B of the Kitchen worksheet. If that row is above row 2, the macro does not change any rows.
Free Microsoft Office alternative

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.

View and edit spreadsheet rows in WPS SpreadsheetsCompatible with Microsoft Excel formats such as XLSX and XLSMFamiliar spreadsheet interface for an easier transitionFree, lightweight tools for everyday Office work
microsoft office alternative - wps office

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.