logo
search
VBA & Macro Problems

How to Move Rows with Specific Text to the Bottom in Excel using VBA

Chanuka GeekiyanageChanuka Geekiyanage Oct 15, 2026 869 views

Question details

The user needs to use a VBA macro to move all rows containing a specific text string to the bottom of the dataset.

How to Move Rows Containing Specific Text to the Bottom Using Excel VBA
Product
Excel
Device & OS
not provided
Scenario
Sorting or organizing data by identifying specific text in a column and relocating those entire rows to the end of the worksheet.
Observed behavior
Rows matching the designated text criteria are successfully moved to the bottom of the active worksheet without any matching rows being skipped during the process.
Before you start

Ensure your workbook is saved as an Excel Macro-Enabled Workbook (.xlsm) and that the Developer tab is visible on your ribbon so you can access the Visual Basic editor.

Solution 1Recommended

Use a VBA Macro with a Reverse Loop

Create and run a VBA script that loops backwards through your dataset to safely cut and paste matched data to the bottom.

When deleting or moving rows in Excel using VBA, it is crucial to loop backwards (from the bottom row to the top row). If you loop forward, moving a row shifts subsequent rows upward. This changes their row numbers and causes the macro to skip the next row in the sequence.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) window.

2
Insert a New Module

In the top menu, click 'Insert' and select 'Module' to create a blank script window.

3
Write the Macro Code

Write a script that defines the last used row in Column A, then use a 'For i = lastRow To 1 Step -1' loop to check if the cell value matches your specific text (e.g., 'Basic model'). If it matches, cut the entire row and paste it after the current last row.

4
Run the Macro

Close the VBA editor. Press ALT + F8 to open the Macro dialog, select your newly created macro, and click 'Run' to move the rows.

Use a VBA Macro with a Reverse Loop
Customize the Code: Remember to change the target text 'Basic model' in your code to the exact phrase you are searching for, and adjust the column reference if your target text is not located in Column A.

Run VBA Macros Seamlessly in WPS Spreadsheet

WPS Office provides excellent support for VBA macros, allowing you to run, edit, and create data automation scripts just like you would in Microsoft Excel.

  1. 1. Open your macro workbook: Launch WPS Spreadsheet and open your .xlsm file containing the data.
  2. 2. Access Developer Tools: Navigate to the 'Developer' tab on the top ribbon menu.
  3. 3. Open the VBA Editor: Click 'Visual Basic' or 'Macros' to access the editing environment.
  4. 4. Run the row-moving macro: Paste your reverse-loop script into the module and execute it to organize your rows.
Full support for standard Excel VBA scripts and macrosHighly compatible with Microsoft Excel .xlsm and .xlsb macro-enabled formatsLightweight application that processes heavy macro loops quicklyBuilt-in Developer tools for easy macro management and editing
microsoft office alternative - wps office

Frequently Asked Questions

Why does my macro skip rows when moving them?

If your macro loops forward (e.g., from row 1 to 100), moving row 5 to the bottom shifts row 6 up to position 5. The loop then advances to row 6, completely missing the data that shifted into row 5. Using a reverse loop (Step -1) prevents this indexing issue.

Can I move rows based on a partial text match instead of an exact match?

Yes, you can modify your VBA code to use the `InStr` function or the `Like` operator. This allows the macro to check if a cell contains a specific word anywhere in the text, rather than requiring an exact cell match.

How do I save an Excel file that contains a macro?

You must save the file as an Excel Macro-Enabled Workbook (.xlsm). If you attempt to save it as a standard Excel Workbook (.xlsx), Excel will display a warning, and all VBA code will be permanently removed upon saving.