How to Remove Duplicate Line Values from Excel Cells with VBA
Question details
The user needs to remove duplicate text lines separated by line breaks within individual Excel cells using a VBA macro.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Cleaning up spreadsheet data where single cells contain multiple lines of text, some of which are duplicates, and needing to retain only one instance of each unique line.
- Observed behavior
- Cells in a specific column contain duplicate strings separated by line feeds. These strings need to be parsed, filtered for uniqueness, and reconstructed.
Before running the macro, ensure the Developer tab is enabled in your ribbon and create a backup of your workbook, as VBA macro actions cannot be undone.
Use VBA Scripting.Dictionary to Deduplicate In-Cell Lines
This method uses the VBA Scripting.Dictionary object to efficiently parse and remove duplicate lines within individual cells.
A Scripting.Dictionary stores items with unique keys. By splitting the cell content by line-feed characters and assigning each line as a dictionary key, duplicates are automatically ignored.
Once all unique keys are collected, they are rejoined using the line-feed character and written back into the cell.
Press ALT + F11 to open the Visual Basic for Applications (VBA) editor in your spreadsheet.
Click on 'Insert' in the top menu and select 'Module' to create a blank workspace for your code.
Create a macro that loops through your target cells. Use the Split() function to divide the cell text by 'vbLf' (line feed). Add each split line into a Scripting.Dictionary by assigning 'd(v) = Null', which ensures only unique keys are saved.
Reconstruct the cell content by joining the dictionary keys back together using 'Join(d.Keys, vbLf)' and assign this new string back to the cell.

Run VBA Macros to Clean Data Easily in WPS Spreadsheet
WPS Office provides robust support for VBA macros, allowing you to run powerful scripts like Scripting.Dictionary to clean up your cell data without any hassle.
- 1. Open your Workbook: Launch WPS Spreadsheet and open your macro-enabled workbook.
- 2. Access the Macro Editor: Navigate to the Tools tab on the ribbon and click on the Macro button to access the VBA Editor.
- 3. Run the Deduplication Script: Paste your dictionary-based deduplication script into a new module and execute it directly to clean your cell data.

Frequently Asked Questions
What character represents an in-cell line break in VBA?
In VBA, an in-cell line break is typically represented by the `vbLf` (line feed) constant. When splitting or joining cell text with line breaks, you should use `vbLf` as the delimiter.
Can I undo a VBA macro after it removes the duplicates?
No, actions performed by a VBA macro typically clear the Excel undo history. You should always save a copy or backup of your data before running the script.
Why does assigning Null to a dictionary key prevent duplicates?
A VBA Scripting.Dictionary requires all keys to be unique. When you assign a value (like Null) to an existing key, it simply overwrites the value for that key instead of creating a new entry, effectively leaving only one instance of the line.




