Remove duplicate in below cell, stop if cell is blank

JTSID

New Member
Joined
Oct 3, 2017
Messages
1
I am looking for a way to remove duplicates from a column. I know how to do it from the end of column up, but I need the first instant of the value to stay. Also I need the code to stop once it hits an empty cell. Column is segmented into different section spit by empty cell. I do not want to shift cells up.

Example 1:

T204 Center Drill
T201 Rough Turn
T201 Rough Turn
T201 Rough Turn

End Results:
T204 Center Drill
T201 Rough Turn

Example 2 with blank cell in between:
T204 Center Drill
T201 Rough Turn

T201 Rough Turn
T201 Rough Turn

End Results:
T204 Center Drill
T201 Rough Turn

T201 Rough Turn

Thank You,

JT
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
There might be a more eloquent solution to this, but this seems to work. Make a copy of your workbook and test the code out on the copy.

Code:
Sub removeDupes()


Dim r As Range
Dim cel As Range

Set cel = Range("A7") 'Set to first cell in first column

Do Until cel.Value = ""
    Set r = cel.CurrentRegion
    r.RemoveDuplicates 2, xlNo
    Set cel = r.Cells(1, 1)
    Set cel = cel.End(xlDown)
    Set cel = cel.End(xlDown)
Loop

End Sub
 
Upvote 0
This solution is a bit better. Should be faster over large datasets and wont run into having multiple blank rows between groups like the first solution does.

You will need to go into the VB Editor and add a reference to Microsoft Scripting Runtime for it to work.

Code:
Sub removeDupesII()
Dim SD As New Dictionary
Dim LR As Long
Dim cnt As Long
Dim r As Range
Dim AR()
Dim Res()

cnt = 1
LR = Range("A" & Rows.Count).End(xlUp).Row()

Set r = Range("A7:B" & LR)
AR = r.Value

For i = 1 To UBound(AR)
    If AR(i, 1) <> "" And i <> UBound(AR) Then
        If Not SD.Exists(AR(i, 1)) Then
            SD.Add AR(i, 1), AR(i, 2)
        End If
    Else
        For j = 0 To SD.Count - 1
            ReDim Preserve Res(1 To cnt)
            Res(cnt) = SD.Keys(j) & "|" & SD.Items(j)
            cnt = cnt + 1
        Next j
        SD.RemoveAll
        ReDim Preserve Res(1 To cnt)
        Res(cnt) = ""
        cnt = cnt + 1
    End If
Next i

r.ClearContents
Set r = r.Resize(UBound(AR) - 1, 1)
r.Value = Application.Transpose(Res)
r.TextToColumns Destination:=r.Cells(1, 1), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="|", FieldInfo:=Array(Array(1, 1), Array(2, 1)), TrailingMinusNumbers:=True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,577
Messages
6,173,163
Members
452,503
Latest member
AM74

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