How to Insert a Row Above Matching Text Using Excel VBA
Question details
The user wants to write an Excel VBA macro that searches for a specific text string, inserts a new row directly above the matched cell, and applies the formatting of an adjacent row.
- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating spreadsheet layouts where a new row must be inserted dynamically based on the location of a specific text anchor.
- Observed behavior
- Initial attempts using an array comparison failed silently without executing the insertion. The user needs the correct Range.Find method to accurately locate the exact text string and perform the row insertion.
Ensure your workbook is saved as an Excel Macro-Enabled Workbook (.xlsm) and that you have enabled macros in the Developer tab to allow the VBA code to run.
Use Range.Find to Locate Text and Insert a Row
The most reliable way to find specific text and manipulate the surrounding rows is by referencing the worksheet directly and utilizing the Range.Find method.
Avoid comparing worksheet name arrays directly to strings. Instead, you should target the specific worksheet and use the Find method to ensure an exact text match before executing the row insertion.
Open the VBA editor (Alt + F11) and reference your target worksheet directly. For example, use `Set ws = ThisWorkbook.Worksheets("Career Plan 2022")` to ensure the macro runs on the correct sheet.
Set up the search parameter by using `Set foundCell = ws.UsedRange.Find(What:="Your Target Text", LookAt:=xlWhole)`. This searches the sheet for an exact match of your specified character string.
Add an IF statement to confirm a match was found: `If Not foundCell Is Nothing Then`. Inside this block, use `foundCell.EntireRow.Insert` to insert a new row directly above the matched cell.
To copy the formatting, select the neighboring row using Offset and apply PasteSpecial. For example: `foundCell.Offset(-2, 0).EntireRow.Copy` followed by `foundCell.Offset(-1, 0).EntireRow.PasteSpecial Paste:=xlPasteFormats`.

Automate Row Insertions with WPS Office Spreadsheets
WPS Office provides robust and seamless support for VBA macros. You can easily run your custom scripts to find text, insert rows, and format data just as you would in Microsoft Excel.
- 1. Open Your Workbook in WPS: Launch WPS Office and open your macro-enabled spreadsheet.
- 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon and click on 'VBA Editor' to open the scripting environment.
- 3. Insert and Run the Macro: Create a new module, paste your Range.Find row insertion code, and press F5 to execute the automation instantly.
- 4. Save as XLSM: Go to Menu > Save As, and select the `.xlsm` format to securely preserve your macro functionality for future use.

Frequently Asked Questions
Why does my VBA code do nothing when I click the run button?
This usually happens if your `If` condition fails silently. If the Range.Find method cannot find an exact match due to hidden leading/trailing spaces or incorrect casing, or if your array variables are incorrectly configured, the code inside the block is skipped and nothing happens.
How do I insert a row below the matching text instead of above it?
To insert a row directly below the matched cell, modify your code to use `foundCell.Offset(1, 0).EntireRow.Insert`. The Offset method dynamically targets the row beneath your match.
Can I make Range.Find search for partial text matches?
Yes. You can change the `LookAt` parameter in your Find method from `xlWhole` to `xlPart`. This allows the macro to locate and trigger on cells that contain the target text string alongside other text.
How do I loop this code to insert rows above every instance of the matched text?
To find all instances, you will need to utilize the `Range.FindNext` method inside a `Do...While` loop. Be sure to search from the bottom of the worksheet upwards (using a reverse `For` loop or specific search direction) so that inserting rows doesn't disrupt the index of unchecked rows.




