VBA & Macro Problems

How to Prevent a VBA Macro from Adding Duplicate Rows in WPS Spreadsheets

Aamir Naveed AkramAamir Naveed Akram Nov 13, 2026 869 views

Question details

The user needs to stop a VBA macro from inserting duplicate rows into a target worksheet when a matching record already exists.

How to Prevent a VBA Macro from Adding Duplicate Rows in WPS Spreadsheets
Product
WPS Spreadsheets
Device & OS
not provided
Scenario
A VBA macro loops through source data and writes rows to a target worksheet. When the macro runs repeatedly, it should update existing matching records instead of adding duplicates.
Observed behavior
The macro currently adds a new row even when a matching record already exists, creating duplicate entries.
Before you start

Open the workbook in WPS Spreadsheets and press Alt+F11 to access the VBA editor. Identify the key columns that define a duplicate record before editing the macro.

Solution 1Recommended

Add a Boolean duplicateFound flag before adding a row

Use this method when your macro loops through the target worksheet and you need to update an existing row or insert a new row only when no matching key columns are found.

The Boolean flag works as a switch: set it to False before the search loop, set it to True when a match is found, and add a new row only when the flag remains False. Compare consistent data types and use Trim on text fields to avoid false mismatches.

1
Open the VBA editor and locate the macro

In WPS Spreadsheets, press Alt+F11 to open the VBA editor. In the Project Explorer, double-click the module that contains the macro that writes rows to the target worksheet.

2
Add the duplicateFound check and comparison loop

Before the loop, declare and reset the flag. Example: Dim duplicateFound As Boolean duplicateFound = False For currentRow = 6 To wsCurrent.Cells(wsCurrent.Rows.Count, "B").End(xlUp).Row If wsCurrent.Cells(currentRow, "B").Value = wsRROB.Cells(i, "AE").Value And _ wsCurrent.Cells(currentRow, "D").Value = wsRROB.Cells(i, "C").Value And _ wsCurrent.Cells(currentRow, "E").Value = wsRROB.Cells(i, "X").Value Then

3
Update the existing row and set the flag

Inside the matched condition, update the required value, set duplicateFound to True, and exit the loop. Example: wsCurrent.Cells(currentRow, "F").Value = wsRROB.Cells(i, "F").Value duplicateFound = True Exit For

4
Add a new row only when no match was found

After the loop, use If Not duplicateFound Then to calculate the next target row and write the new record. Example: If Not duplicateFound Then newRow = wsCurrent.Cells(wsCurrent.Rows.Count, "B").End(xlUp).Row + 1 wsCurrent.Cells(newRow, "B").Value = wsRROB.Cells(i, "AE").Value End If

Add a Boolean duplicateFound flag before adding a row
Duplicate rows prevented: After this change, the macro updates the existing row when the key columns match and inserts a new row only when duplicateFound remains False.
WPS Spreadsheets VBA fix

Prevent duplicate rows in your WPS Spreadsheets VBA macro

Open the VBA editor in WPS Spreadsheets, add a duplicateFound Boolean check around the row-writing loop, and make the macro compare key columns before updating or inserting rows.

  1. 1. Open your workbook in WPS Spreadsheets: Launch WPS Office and open the workbook that contains the macro. Enable macros if you are prompted.
  2. 2. Open the VBA editor: Press Alt+F11 to open the VBA editor and select the module that writes rows to the current worksheet.
  3. 3. Add the duplicateFound check: Before the search loop, set duplicateFound to False. Compare the key columns, set duplicateFound to True when a match is found, and add a new row only when the flag remains False.
  4. 4. Run the macro to verify: Return to WPS Spreadsheets and run the macro again. Confirm that existing rows update and that no duplicate rows are added.
Stops repeated macro runs from creating duplicate rows in shared workbooks.Keeps .xlsm, .xls, and .xlsx workbook compatibility when opening and saving files.Free, lightweight Office suite with a familiar VBA editing environment.No need to rewrite the whole macro; a small Boolean check is enough.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA macro keep inserting duplicate rows every time I run it?

The macro likely inserts a new row without first checking whether the target worksheet already contains a matching record. Add a Boolean duplicateFound flag before the insertion logic to search for matching key columns and skip insertion when a match exists.

Can I check more than one key column for duplicates?

Yes. In the If condition, compare every column that defines a unique record, such as columns B, D, and E. Use consistent data types and apply Trim to text values to avoid false mismatches.

Does WPS Spreadsheets support editing and running VBA macros?

Yes. WPS Spreadsheets includes a VBA editor and macro support. Press Alt+F11 to open the editor and modify the macro logic. Macro availability can vary by version, but the Boolean flag approach works in standard VBA.

What should I check if the macro still adds duplicates after adding the flag?

Verify that the key columns in the If condition exactly match the fields written to the target sheet and that the loop range covers all existing data rows. For text values, wrap the comparisons in CStr(Trim(...)) to remove leading or trailing spaces.