How to Select a Dynamic Excel Range Using Column T in VBA
Question details
The user needs to create a VBA macro that selects a dynamic rectangular range starting from cell J5, extending horizontally to the last used header column in row 5, and extending vertically to the last used row determined specifically by column T.

- Product
- Microsoft Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Automating data selection in a spreadsheet where the number of rows and columns changes dynamically, specifically using a distinct column (Column T) to dictate the bottom boundary of the selection.
- Observed behavior
- The user wants a continuous rectangular selection but encounters application-defined errors or incomplete selections when attempting to use the Union method, which mistakenly groups individual corner cells rather than the bounding box.
Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have the Developer tab enabled on your ribbon to access the VBA editor.
Use the VBA Range Object with Start and End Cells
The most effective way to select a dynamic, continuous rectangular range is to define the top-left cell and bottom-right cell using the Range object rather than the Union method.
Using the Union method will only select the discrete start and end cells (e.g., J5 and the final cell in row/column) rather than the entire block of data between them. To select a solid rectangular area, you must pass the start and end cells directly into the Range object.
In this scenario, column T determines the final row, while row 5 determines the final column. By dynamically finding these limits, we can pass them into our Range object.
Press Alt + F11 on your keyboard or navigate to the Developer tab and click on 'Visual Basic'.
In the Project Explorer window on the left side, right-click on your workbook name, select 'Insert', and then click 'Module'.
Paste the following correct script into the module window: ```vba Sub SelectDynamicRange() Dim ws As Worksheet Dim lastRow As Long Dim lastColumn As Long Dim rng As Range Set ws = ActiveSheet lastRow = ws.Cells(ws.Rows.Count, "T").End(xlUp).Row lastColumn = ws.Cells(5, ws.Columns.Count).End(xlToLeft).Column Set rng = ws.Range(ws.Cells(5, 10), ws.Cells(lastRow, lastColumn)) rng.Select End Sub ```
Press F5 or click the 'Run' button in the toolbar to execute the macro. The dynamic range starting at J5 down to the last row in Column T will now be correctly selected.

Run Dynamic Range VBA Macros Easily in WPS Office
WPS Spreadsheet provides excellent support for VBA and macros, allowing you to run complex scripts like dynamic range selection seamlessly. It is an outstanding free alternative that retains familiar developer tools and commands for managing large datasets.
- 1. Install WPS Office: Download and install WPS Office, launch WPS Spreadsheet, and open your macro-enabled (.xlsm) file.
- 2. Enable the Developer Tab: Navigate to the ribbon menu, ensure the Developer tab is active, and click on 'VBA Editor'.
- 3. Run Your Macro: Insert your dynamic range selection code into a module and run it directly to manipulate your spreadsheet data.

Frequently Asked Questions
Why does the Union method fail to select a dynamic range?
The Union(Cell1, Cell2) method is designed to group specific, non-contiguous cells together into a single selection. It does not select the cells located between them. To select a continuous block or rectangle, you must use Range(Cell1, Cell2) instead.
How does VBA determine the last used row in a specific column?
The command Cells(Rows.Count, "T").End(xlUp).Row simulates pressing Ctrl + Up Arrow from the very bottom of the worksheet in column T. It stops at the first cell that contains data, giving you the true last used row for that specific column regardless of empty cells above it.
What if my header row is different than row 5?
If your headers are located in a different row, you simply need to change the row index in the lastColumn calculation and the Range start cell. For example, if headers are in row 1, update the column variable to ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column.




