Excel VBA: Set a Range from B2 to the Last Nonblank Cell
Question details
Create a VBA range that starts at B2 and automatically ends at the last populated cell in column B.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A macro needs to process a column whose number of entries changes over time.
- Observed behavior
- The range should expand or contract with the data instead of requiring a manually updated reference such as B2:B5.
Check the worksheet name and confirm that B1 is a header and the data starts at B2. If you want to keep the macro in the workbook, save it as an Excel Macro-Enabled Workbook (.xlsm).
Find the last row in column B and set a dynamic VBA range
Use this approach when column B may contain blank cells between entries.
The macro searches upward from the bottom of column B, then sets AISLE from B2 through the last populated row. Any blank cells between those endpoints remain in the range.
In Excel, open the workbook and choose Developer > Visual Basic. In the editor, choose Insert > Module.
Paste this code into the module and replace Sheet1 with the actual worksheet name: Sub SetAisleRange() Dim ws As Worksheet Dim AISLE As Range Dim lastRow As Long Set ws = ThisWorkbook.Worksheets("Sheet1") If IsEmpty(ws.Cells(ws.Rows.Count, "B")) Then lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row Else lastRow = ws.Rows.Count End If If lastRow < 2 Then MsgBox "No data in column B below the header." Exit Sub End If Set AISLE = ws.Range("B2", ws.Cells(lastRow, "B")) MsgBox "Range set to " & AISLE.Address End Sub
In Excel, choose Developer > Macros, select SetAisleRange, and click Run. Check the displayed address, then replace the final MsgBox with the actions your macro should perform on AISLE.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative for opening and editing spreadsheets. Its familiar interface and Excel format compatibility make it easy to move everyday spreadsheet work from Microsoft Office.

Frequently Asked Questions
Will the VBA range include blank cells between entries?
Yes. The range from B2 to the last populated cell includes every cell between those endpoints, including blanks.
What happens if there is no data below B1?
The example checks whether the last populated row is before row 2. If it is, the macro displays a message and exits without setting AISLE.
How do I use a different starting cell or column?
In the VBA editor, change both references to column B and replace B2 with your starting cell. Also update the lastRow check to match the starting row.
Why should I qualify the Range and Cells references with a worksheet?
Using ws.Range and ws.Cells ensures the macro reads from the worksheet specified by ThisWorkbook.Worksheets("Sheet1"), rather than relying on whichever sheet is active.
Can I use End(xlDown) instead?
You can use End(xlDown) for a continuous block, but gaps or a missing entry can make it stop at an unexpected cell. Searching upward from the bottom is safer when entries may contain blanks.




