Remove duplicate by selected

Sefty

Board Regular
Joined
Apr 5, 2022
Messages
68
Office Version
  1. 365
Platform
  1. Windows
Hello,
i need help, how to delete duplicates, and only select the last data. For example, if there are 3 data that are the same, then only the third data is taken
data that needs to be deleted per row, duplicates based on column B but need to delete all rows and only leave 1 last row.
Thanks
Book1
ABCDEF
1Before
2HariAbc
3Sundayaab
4Sundayfgjj
5Sunday12jj
6Monday457
7Monday901
8Friday125
9Friday146
10
11
12After
13HariAbc
14Sunday12jj
15Monday901
16Friday146
17
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B3:B9,B12Cell ValueduplicatestextNO
B14:B16Cell ValueduplicatestextNO
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Like this?
VBA Code:
Sub test()
  Dim lRow As Long
  lRow = Cells(Rows.Count, "B").End(xlUp).Row
  Application.ScreenUpdating = False
  For i = lRow To 2 Step -1
    If Cells(i - 1, "B").Value2 = Cells(i, "B").Value2 Then
      Rows(i - 1).EntireRow.Delete
      i = i + 1
    End If
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Like this?
VBA Code:
Sub test()
  Dim lRow As Long
  lRow = Cells(Rows.Count, "B").End(xlUp).Row
  Application.ScreenUpdating = False
  For i = lRow To 2 Step -1
    If Cells(i - 1, "B").Value2 = Cells(i, "B").Value2 Then
      Rows(i - 1).EntireRow.Delete
      i = i + 1
    End If
  Next
  Application.ScreenUpdating = True
End Sub
Wow
Like this?
VBA Code:
Sub test()
  Dim lRow As Long
  lRow = Cells(Rows.Count, "B").End(xlUp).Row
  Application.ScreenUpdating = False
  For i = lRow To 2 Step -1
    If Cells(i - 1, "B").Value2 = Cells(i, "B").Value2 Then
      Rows(i - 1).EntireRow.Delete
      i = i + 1
    End If
  Next
  Application.ScreenUpdating = True
End Sub
I'm sorry, if i want remove duplicate and selected highest "class" on the column "D" what to do
Thank you
Book1
ABCDEF
1DayKategori 1ClassKategori 2
2Sunday890
3deleteSunday341
4deleteSunday341
5Monday 148
6deleteMonday 219
7deleteMonday 321
8deleteFriday722
9Friday534
10
11
12
13
Sheet1
 
Upvote 0
@Flashbond
FWIW
  • '.EntireRow' is not required since Rows(i - 1) already is an entire row
  • 'i = i + 1' is not required
Rich (BB code):
  For i = lRow To 2 Step -1
    If Cells(i - 1, "B").Value2 = Cells(i, "B").Value2 Then
      Rows(i - 1).EntireRow.Delete
      i = i + 1
    End If
  Next

@Sefty
About how many rows is your real data set likely to be?
 
Upvote 0
@Flashbond
FWIW
  • '.EntireRow' is not required since Rows(i - 1) already is an entire row
  • 'i = i + 1' is not required
Rich (BB code):
  For i = lRow To 2 Step -1
    If Cells(i - 1, "B").Value2 = Cells(i, "B").Value2 Then
      Rows(i - 1).EntireRow.Delete
      i = i + 1
    End If
  Next

@Sefty
About how many rows is your real data set likely to be?
There are 6000 data i have

sorry if i want remove duplicate and selected highest "class" on the column "D" what to do
Book1
ABCDEF
1DayKategori 1ClassKategori 2
2Sunday890
3deleteSunday341
4deleteSunday341
5Monday 148
6deleteMonday 219
7deleteMonday 321
8deleteFriday722
9Friday534
10
11
12
13
Sheet1
 
Upvote 0
Then something like this:
VBA Code:
Sub test()
  Dim lRow As Long
  lRow = Cells(Rows.Count, "B").End(xlUp).Row
  Application.ScreenUpdating = False
  For i = lRow To 2 Step -1
    If Cells(i - 1, "B").Value2 = Cells(i, "B").Value2 Then
      If Cells(i - 1, "D").Value2 <= Cells(i, "D").Value2 Then
        Rows(i - 1).Delete
      Else
        Rows(i).Delete
      End If
    End If
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
@Flashbond
FWIW
  • '.EntireRow' is not required since Rows(i - 1) already is an entire row
  • 'i = i + 1' is not required
Rich (BB code):
  For i = lRow To 2 Step -1
    If Cells(i - 1, "B").Value2 = Cells(i, "B").Value2 Then
      Rows(i - 1).EntireRow.Delete
      i = i + 1
    End If
  Next

@Sefty
About how many rows is your real data set likely to be?
OMG,

I didn't know that I can delete the entire row without specifying it if it was a row..
I don't know why I went with i = i + 1. I think I was afraid of skipping a row.
 
Last edited by a moderator:
Upvote 0
Then something like this:
VBA Code:
Sub test()
  Dim lRow As Long
  lRow = Cells(Rows.Count, "B").End(xlUp).Row
  Application.ScreenUpdating = False
  For i = lRow To 2 Step -1
    If Cells(i - 1, "B").Value2 = Cells(i, "B").Value2 Then
      If Cells(i - 1, "D").Value2 <= Cells(i, "D").Value2 Then
        Rows(i - 1).Delete
      Else
        Rows(i).Delete
      End If
    End If
  Next
  Application.ScreenUpdating = True
End Sub
WOW its works, thanks (y) 👏
 
Upvote 0
Please keep an eye on this thread. @Peter_SSs may come up with a faster solution
I think that it could be done faster, but I'm not sure with 6,000 rows (not really very big) that the difference would be noticeable.

One other comment about your codes: I note that you have declared lRow as long but left i undeclared (& therefore Variant). I would recommend declaring all variables (& forcing yourself to do so with the 'Require Variable Declaration' setting in the vba Options).

@Sefty
Is the supplied code fast enough? In other words, is there any noticeable wait-time while the code runs?
 
Upvote 0

Forum statistics

Threads
1,215,442
Messages
6,124,886
Members
449,194
Latest member
ronnyf85

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