How to Move Rows with Specific Text to the Bottom in Excel using VBA
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.

- 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.
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.
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.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) window.
In the top menu, click 'Insert' and select 'Module' to create a blank script window.
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.
Close the VBA editor. Press ALT + F8 to open the Macro dialog, select your newly created macro, and click 'Run' to move the rows.

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. Open your macro workbook: Launch WPS Spreadsheet and open your .xlsm file containing the data.
- 2. Access Developer Tools: Navigate to the 'Developer' tab on the top ribbon menu.
- 3. Open the VBA Editor: Click 'Visual Basic' or 'Macros' to access the editing environment.
- 4. Run the row-moving macro: Paste your reverse-loop script into the module and execute it to organize your rows.

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.




