Help Excel formula eliminate duplicate values and keep only 2 identical rows.

Nguyen Anh Dung

Board Regular
Joined
Feb 28, 2020
Messages
180
Office Version
  1. 2016
Platform
  1. Windows
as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9 rows,etc.... How to delete duplicate values and keep only 2 rows
1585376962735.png

link file:
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
VBA Code:
Sub v()
Dim rng As Range: Set rng = Range("A2:A" & Cells(Rows.Count, "A").End(3).Row)
With rng.Offset(0, 3)
    .Formula = "=IF(COUNTIF(A$2:A2,A2)>2,""d"",1)"
    .SpecialCells(xlCellTypeFormulas, 2).EntireRow.Delete
    .ClearContents
End With
End Sub
 
Upvote 0
Code:
VBA Code:
Sub v()
Dim rng As Range: Set rng = Range("A2:A" & Cells(Rows.Count, "A").End(3).Row)
With rng.Offset(0, 3)
.Formula = "=IF(COUNTIF(A$2:A2,A2)>2,""d"",1)"
.SpecialCells(xlCellTypeFormulas, 2).EntireRow.Delete
.ClearContents
End With
End Sub
Thanks you so much!!! you can help me for this case run multiple file in folder!!!
 
Upvote 0
Code:
VBA Code:
Sub v()
Dim rng As Range: Set rng = Range("A2:A" & Cells(Rows.Count, "A").End(3).Row)
With rng.Offset(0, 3)
.Formula = "=IF(COUNTIF(A$2:A2,A2)>2,""d"",1)"
.SpecialCells(xlCellTypeFormulas, 2).EntireRow.Delete
.ClearContents
End With
End Sub
Thanks you so much!!! you can help me for this case run multiple file in folder!!!
Since this is a different topic, it's better that you start a new thread.
 
Upvote 0
In your CSV, what are all those 1's in column D ?
You must have entered the formula incorrectly - please note the absolute row ref (A$2):
=IF(COUNTIF(A$2:A2,A2)>2,"d",1)

Thank you and best regards, I have more question: if keep only 2 rows the same time, but getting the first and the last row?
 
Upvote 0
Assuming column A is in sequence :
VBA Code:
Sub v()
With Range("D2:D" & Cells(Rows.Count, "A").End(3).Row)
    .Formula = "=IF((A2=A1)*(A2=A3),""d"",1)"
    .SpecialCells(xlCellTypeFormulas, 2).EntireRow.Delete
    .ClearContents
End With
End Sub
 
Upvote 0
t
Assuming column A is in sequence :
VBA Code:
Sub v()
With Range("D2:D" & Cells(Rows.Count, "A").End(3).Row)
    .Formula = "=IF((A2=A1)*(A2=A3),""d"",1)"
    .SpecialCells(xlCellTypeFormulas, 2).EntireRow.Delete
    .ClearContents
End With
End Sub
thanks you!!!
 
Upvote 0
just for fun with Power Query

first two duplicates from each group (part of data)
IndexDate/timeLatitudeLongitude
12020-03-18 03:28:1010.8208494106.6307964
22020-03-18 03:28:1010.8208495106.6307963
132020-03-18 03:28:1110.8208499106.6307958
142020-03-18 03:28:1110.82085106.6307957
322020-03-18 03:28:1210.820851106.630795
332020-03-18 03:28:1210.8208511106.6307951
482020-03-18 03:28:1310.8208515106.6307952
492020-03-18 03:28:1310.8208515106.6307952
642020-03-18 03:28:1410.8208514106.6307956
652020-03-18 03:28:1410.8208514106.6307956
822020-03-18 03:28:1510.820852106.6307959
832020-03-18 03:28:1510.8208519106.630796

first and middle duplicates from each group (part of data)
IndexDate/timeLatitudeLongitude
12020-03-18 03:28:1010.8208494106.6307964
62020-03-18 03:28:1010.8208496106.6307961
132020-03-18 03:28:1110.8208499106.6307958
212020-03-18 03:28:1110.8208502106.6307954
322020-03-18 03:28:1210.820851106.630795
392020-03-18 03:28:1210.8208514106.630795
482020-03-18 03:28:1310.8208515106.6307952
552020-03-18 03:28:1310.8208513106.6307955
642020-03-18 03:28:1410.8208514106.6307956
722020-03-18 03:28:1410.820852106.6307958
822020-03-18 03:28:1510.820852106.6307959
892020-03-18 03:28:1510.8208528106.6307964

index visualizing only which records were used
 
Upvote 0
Assuming column A is in sequence :
VBA Code:
Sub v()
With Range("D2:D" & Cells(Rows.Count, "A").End(3).Row)
    .Formula = "=IF((A2=A1)*(A2=A3),""d"",1)"
    .SpecialCells(xlCellTypeFormulas, 2).EntireRow.Delete
    .ClearContents
End With
End Sub
Hi footoo,
In this case I have 16 rows or 18 rows or 19 rows duplicate: if keep only 5 rows the same time, but getting the first and four, eight,
twelve and the last row?
 
Upvote 0
VBA Code:
Sub v()
With Range("D2:D" & Cells(Rows.Count, "A").End(3).Row)
    .Formula = "=IF((A2<>A1)+(COUNTIF(A$2:A2,A2)=4)+(COUNTIF(A$2:A2,A2)=8)+(COUNTIF(A$2:A2,A2)=12)+(A2<>A3),""k"",1)"
    .SpecialCells(xlCellTypeFormulas, 1).EntireRow.Delete
    .ClearContents
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top