How to Remove Duplicates But Keep First Instance in Excel?


Duplicate entries are likely a problem if you've ever worked with large datasets or gotten data from several different sources. It is crucial to clean up your data by deleting duplicate values because these duplicates might clog up your spreadsheets and compromise data accuracy. This step-by-step tutorial will show you how to locate duplicate entries in your Excel sheets and remove them while maintaining the initial instance of each unique entry. By simplifying your data for analysis, reporting, or additional processing, you can maintain data integrity.

This tutorial will give you the skills and information you need to effectively eliminate duplicates from your spreadsheets while maintaining the original data, whether you're new to Excel or an advanced user wishing to improve your data management abilities.

Remove Duplicates But Keep First Instance

Here we will first create a VBA module, then run the module to complete the task. So let us see a simple process to know how you can remove duplicates but keep the first instance in Excel.

Step 1

Consider an Excel sheet where you have a list of duplicate rows.

First, right-click on the sheet name and select View code to open the VBA application.

Right Click > View Code.

Step 2

Then click on Insert and select Module, then copy the below code into the text box.

Insert > Module > Copy.

Code

Sub TrimExcessSpaces()
	Dim Rng As Range
	Dim WorkRng As Range
	Dim dic As Variant
	Set dic = CreateObject("Scripting.Dictionary")
	On Error Resume Next
	xTitleId = "Remove Duplicates Keep"
	Set WorkRng = Application.Selection
	Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type: = 8)
	Set WorkRng = WorkRng.Columns(1)
	For Each Rng In WorkRng.Cells
		dic(Rng.Value) = ""
	Next
	WorkRng.ClearContents
	WorkRng.Range("A1").Resize(UBound(dic.Keys) + 1, 1) = Application.WorksheetFunction.Transpose(dic.Keys)
End Sub

Step 3

Then press F5 to run the module. Then select the range of cells and click OK to complete the task.

This is how you can remove duplicates but keep the first instance in Excel.

Conclusion

In this tutorial, we have used a simple example to demonstrate how you can remove duplicates but keep the first instance in Excel to highlight a particular set of data.

Updated on: 07-Sep-2023

171 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements