VBA & Macro Problems

How to Make an Excel Macro Use Selected Rows Instead of Fixed Ranges

Maira MehtabMaira Mehtab Nov 3, 2026 869 views

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.

How to Make an Excel Macro Use Selected Rows Instead of Fixed Ranges
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the Visual Basic Editor

Press ALT + F11 on your keyboard to open the VBA Editor, then locate your macro in the 'Modules' folder on the left sidebar.

2
Locate Fixed Range References

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'.

3
Replace with Selection.EntireRow

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'.

4
Test the Macro

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.

Modify the VBA Code to Use Selection.EntireRow
Tip: You don't need to select the whole row manually before running the macro. Selecting a single cell in column A is enough; Selection.EntireRow will automatically expand the action to that whole row.

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. 1. Open Your Macro-Enabled File: Launch WPS Spreadsheet and open your existing .xlsm workbook.
  2. 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. 3. Edit the Code: Modify your VBA script using the 'Selection.EntireRow' method and save your changes.
  4. 4. Run the Dynamic Macro: Select the target rows in your worksheet and click 'Macros' from the ribbon to run your updated script.
Fully compatible with Microsoft Excel macro formats (.xlsm).Built-in VBA editor for modifying Range and Selection properties.Free and lightweight alternative for complex spreadsheet automation.Familiar ribbon interface makes finding the Developer tab effortless.
microsoft office alternative - wps office

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.