How to Make an Excel Macro Use Selected Rows Instead of Fixed Ranges
Question details
The user needs to update a recorded macro so it dynamically formats or edits the rows they have selected, rather than modifying the exact fixed ranges recorded initially.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Running a recorded macro on weekly data sets where the target rows change in location and quantity.
- Observed behavior
- The macro currently relies on absolute hardcoded cell references. The goal is to make it dynamically reference and execute on the user's active row selection.
Ensure that the Developer tab is enabled in your spreadsheet application and always save a backup copy of your workbook before testing new VBA code, as macro actions typically cannot be undone.
Modify the VBA Code to Use Selection.EntireRow
Replace the fixed range references in your recorded macro with Selection.EntireRow to apply changes across the entire row of any currently highlighted cells.
When you record a macro, Excel writes the exact cell addresses (absolute references) you clicked. By editing the VBA code to use 'Selection.EntireRow', you instruct the macro to ignore those fixed addresses and instead look at whatever cells you have highlighted right before running it.
Press ALT + F11 on your keyboard to open the VBA Editor, then locate your macro in the 'Modules' folder on the left sidebar.
Scroll through your macro's code and look for lines that select fixed ranges, such as 'Rows("2:5").Select' or 'Range("A2:A5").Select'.
Delete the fixed range reference and replace it with 'Selection.EntireRow'. For example, if you want to delete the rows, change the code to 'Selection.EntireRow.Delete'.
Save the code and return to your worksheet. Highlight a few cells in the rows you want to affect, and run the macro to verify it modifies only your selection.

Use Intersect to Target Specific Columns in Selected Rows
Use the VBA Intersect function when you want the macro to dynamically use the selected rows, but only apply changes to specific columns within those rows.
Run and Edit Macros Seamlessly in WPS Spreadsheet
WPS Office provides excellent support for VBA and macros, allowing you to run, record, and edit custom scripts just like in Microsoft Excel. You can easily adapt your macros to handle dynamic row selections to automate your tasks efficiently.
- 1. Open Your Macro-Enabled File: Launch WPS Spreadsheet and open your existing .xlsm workbook.
- 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon and click on 'Visual Basic' to open the code editor.
- 3. Edit the Code: Modify your VBA script using the 'Selection.EntireRow' method and save your changes.
- 4. Run the Dynamic Macro: Select the target rows in your worksheet and click 'Macros' from the ribbon to run your updated script.

Frequently Asked Questions
Why does my recorded macro always select the same cells?
By default, the macro recorder uses absolute referencing, meaning it records the exact cell addresses you clicked (e.g., A1:B10). To make it dynamic without writing code, you need to turn on the 'Use Relative References' button in the Developer tab before recording.
Can I undo a macro after it formats or deletes selected rows?
No, standard undo features (Ctrl+Z) generally do not work for actions performed by a VBA macro. It is highly recommended to test macros on a backup copy of your data first.
How do I turn on the Developer tab to edit my macro?
Go to File > Options > Customize Ribbon. In the right-hand column, check the box next to 'Developer' and click OK. This will add the Developer tab to your ribbon, giving you access to the Visual Basic editor.
What happens if I use Selection.EntireRow but only select a single cell?
The macro will successfully apply its actions to the entire row that contains the single selected cell. You do not need to highlight the row from edge to edge.




