Conditional removal of duplicates

sucram3

New Member
Joined
Sep 19, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi,

I want to sort a column by removing duplicates, but not all duplicates, only those that appear in sequence, i.e:

1
2
3
3 (I want this removed)
4
2
3
2
2 (I want this removed)
3
4
4 (I want this removed)

Reason for this is that I'm trying to build a tree structure where various instances are used on different places in the structure, but not after each other.

I have searched this forum but haven't found anything that helps.

Thanks.
 
Hi
What about
VBA Code:
Sub test()
Dim a: Dim c&, i&
    c = 1
    a = Range(Cells(1, 1), Cells(1, 1).End(xlDown))
    ReDim b(1 To UBound(a))
    For i = 1 To UBound(a) - 1
        If a(i + 1, 1) <> a(i, 1) Then b(c) = a(i + 1, 1):   c = c + 1
    Next
Cells(2, 2).Resize(c) = Application.Transpose(b)
End Sub

Heder
1​
1​
2​
2​
3​
3​
3​
4​
4​
2​
2​
3​
3​
2​
2​
3​
3​
4​
4​
4​
Hi!

Thank you for this.

In the end I used the filter formula which was proposed a few posts earlier in this thread.

Thank you for taking your time.

BR
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I tried this code and had it running for a while, and while testing 200 rows, it worked great. Although when running it on the complete set, all 250000 rows, it appeared to be a bit time consuming.

I'm so grateful for your time, despite my workbook being a bit too big.

Thank you.

BR
That is to be expected, if you are looping through that many rows! Excel is usually not the tastest tool when working with massive amounts of data.

I don't know if it will make any difference, but you can see if these minor edits help speed it up at all (they certainly won't make it any slower):
VBA Code:
Sub MyDeleteDups()

    Dim lr As Long
    Dim r As Long
   
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
   
'   Find last row in column D with data
    lr = Cells(Rows.Count, "D").End(xlUp).Row
   
'   Loop through all rows backwards
    For r = lr To 17 Step -1
'       Delete current row if value in column D same as value above it
        If Cells(r, "D").Value = Cells(r - 1, "D").Value Then Rows(r).Delete
    Next r
   
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,140
Members
449,098
Latest member
Doanvanhieu

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