VBA & Macro Problems

How to Replace Excel Rows with Matching Records Using VBA

John WilsonJohn Wilson Nov 11, 2026 868 views

Question details

The user wants to write a VBA macro that searches for a specific identifier in a destination sheet, deletes the existing row, and inserts updated matching rows from two other source sheets.

How to Replace Excel Rows with Matching Records Using VBA
Product
Excel / WPS Spreadsheet
Device & OS
not provided
Scenario
Automating the process of updating master records by pulling corresponding row data from multiple source sheets based on a shared unique identifier.
Observed behavior
A VBA script is needed to accurately locate the exact identifier, safely remove the outdated destination row, and copy the new matching rows into the next available space.
Before you start

Ensure you have the exact names of your destination and source worksheets, confirm which column holds your unique identifiers, and save a backup copy of your workbook since macro actions cannot be undone.

Solution 1Recommended

Use VBA Find Method to Delete and Copy Rows

Create a macro that utilizes the Find function with exact matching to locate the target row, deletes it, and dynamically copies updated records from your source sheets.

This method relies on the 'Find' method combined with the 'LookAt:=xlWhole' parameter. This ensures the macro matches the exact identifier rather than a partial string, preventing the deletion of incorrect rows.

After deleting the target row in the destination sheet, the macro calculates the next available blank row and copies the updated rows from your secondary source sheets.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications editor. Go to Insert > Module to create a new blank script window.

2
Declare Variables and Set Worksheets

Define your variables for the worksheets and the identifier. Set references to your destination sheet and the two source sheets (e.g., Sheet2 and Sheet3).

3
Locate and Delete the Destination Row

Use the Find method on the destination sheet's identifier column with 'LookAt:=xlWhole'. If a match is found, use 'EntireRow.Delete' to remove the existing record.

4
Copy Corresponding Rows from Source Sheets

Find the matching identifier in your source sheets. Use a function like 'Cells(Rows.Count, 1).End(xlUp).Row + 1' to determine the next empty row in the destination sheet, then copy and paste the entire row data over.

Verify Worksheet and Range Names: Always confirm that the worksheet names and the identifier column range in your VBA code perfectly match the actual structure of your workbook before running the script.
Advanced Data Processing

Automate Your Workflow with WPS Spreadsheet Macros

WPS Spreadsheet supports advanced macro capabilities, allowing you to seamlessly run VBA scripts to locate, delete, and replace rows across multiple worksheets without performance lagging.

  1. 1. Enable the Developer Tools: Open your workbook in WPS Spreadsheet, go to the Options menu, and ensure the 'Developer' tab is enabled on your ribbon.
  2. 2. Launch the Macro Editor: Click the 'Developer' tab and select 'Visual Basic' or 'Macros' to open the code editor.
  3. 3. Execute the Row Replacement Code: Paste your find-and-replace VBA script into a new module, adjust the sheet names to match your file, and click 'Run'.
Seamless compatibility with Microsoft Excel VBA syntax and objects.Process large datasets rapidly without crashing.Fully compatible with Microsoft Excel .xlsm and .xlsb file formats.Built-in Developer tools for easy code writing and debugging.
microsoft office alternative - wps office

Frequently Asked Questions

Why is my VBA Find method deleting the wrong row?

This commonly occurs if you omit the 'LookAt:=xlWhole' parameter in your Find function. Without it, Excel defaults to partial matches (e.g., searching for '12' might match '123'), leading to incorrect row deletions.

How do I find the next available blank row using VBA?

You can dynamically find the next empty row in a specific column (for example, Column A) by using the code snippet: 'NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1'.

Can I use Undo (Ctrl+Z) after a macro deletes a row?

No, actions executed by a VBA macro clear the Undo history. You cannot use the standard Undo feature to revert changes made by VBA. It is highly recommended to test the macro on a backup copy first.