# Removing duplicates WITHIN cell

Status
Not open for further replies.

#### mawheele

##### New Member
I have the following code for removing duplicates within a cell. It runs through an entire column:

Code:
Sub test()
Dim c As Range, t, u As Long, temp
For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
t = Split(c, ",")
For u = 0 To UBound(t) - 1
If Trim(t(u)) = Trim(t(u + 1)) Then t(u + 1) = "`"
Next u
temp = Application.WorksheetFunction.Trim(Join(t, ", ")) & ","
temp = Application.WorksheetFunction.Trim(Replace(temp, "`,", ""))
c.Offset(, 1) = Left(temp, Len(temp) - 1)
Next
End Sub

However, it only removes the first duplicate. How can I have it remove the second, and third, and possibly more duplicates? Would I need to use Select Case?

### Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

#### QuietRiot

##### Well-known Member
hmm,

I tweaked the code for you but then realized that it only removes duplicates if there next to each other

ie; test,test,cow,cow,cow will result in test,cow

is this what you want or are some cells cow,test,cow, test

cause the code above isn't very good. If this is fine let me know ill post what I tweaked otherwise if I have time ill try to think of a better way

#### QuietRiot

##### Well-known Member
from the looks of it then .. they will always be together 1,1,1,2,4

try this:

Code:
Sub test()
Dim c As Range, t, u As Long, temp, x As Integer
x = 0

Do Until x = 6 'change this to what you consider max number of duplicates. I figured 6 would be fine.

For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
t = Split(c, ",")
For u = 0 To UBound(t) - 1
If Trim(t(u)) = Trim(t(u + 1)) Then t(u + 1) = "`"
Next u
temp = Application.WorksheetFunction.Trim(Join(t, ", ")) & ","
temp = Application.WorksheetFunction.Trim(Replace(temp, "`,", ""))
c.Offset(, 0) = Left(temp, Len(temp) - 1)
Next

x = x + 1

Loop
end sub

Status
Not open for further replies.

Replies
0
Views
283
Replies
0
Views
668
Replies
6
Views
213
Replies
15
Views
512
Replies
16
Views
735

1,181,420
Messages
5,929,801
Members
436,696
Latest member
Mr Rice

### 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.

### Which adblocker are you using?

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

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