ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Remove Duplicate Numbers in One Cell (https://www.excelbanter.com/excel-programming/438493-remove-duplicate-numbers-one-cell.html)

Lucky[_6_]

Remove Duplicate Numbers in One Cell
 
My worksheet has one column in which each cell has one and two-digit
numbers separated by spaces, such as

1 18 22 5 1 31 8 11 6 1

I want to remove all the duplicate numbers in each cell, leaving only
one instance of each number in there. (1 and 11 are different
numbers, of course). I've tried a few solutions, but they're clunky
and inelegant and take a long time to do these thousands of cells.

Anyone have a better way?
Thanks,
Lucky

joel[_555_]

Remove Duplicate Numbers in One Cell
 

This isn't eligant but it is pretty straight forward. Use split to put
the numbers into an array. Perform a Buble Test on the array by
comparing each element with all the other element. If a duplicate is
found then put replace the number with an X. Then remove all the
numbers from the array.

Chip Pearson has some solutions for counting the duplicates and non
duplicate in the string. He also has methods for getting duplices when
each number is in a seperate cells. You could use TexttoColumn method
and then use one of Chip's solutions. If you where going to use
TextTocolumns I would then use Advance filter to get unique values.


Sub test()

'Date in cells with spaces are treat as string
'Use the line below for testing
MyArray = "1 18 22 5 1 31 8 11 6 1"

NumberArray = Split(MyArray, " ") 'split line around spaces

For i = LBound(NumberArray) To UBound(NumberArray)
'remove any spaces in the number
NumberArray(i) = Val(Trim(NumberArray(i)))
Next i

'replace all duplicates with empty string
For i = LBound(NumberArray) To (UBound(NumberArray) - 1)
For j = (i + 1) To UBound(NumberArray)
If NumberArray(i) = NumberArray(j) Then
NumberArray(j) = "X"
End If

Next j
Next i

MyArray = ""
For i = LBound(NumberArray) To UBound(NumberArray)
If NumberArray(i) < "X" Then
If MyArray = "" Then
MyArray = NumberArray(i)
Else
MyArray = MyArray & " " & NumberArray(i)
End If
End If
Next i

End Sub


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=170687

Microsoft Office Help


Lucky[_6_]

Remove Duplicate Numbers in One Cell
 
Thanks, Joel! I was onto the array part, but Split was the piece I
was missing. This should work great for me.
Lucky

Ron Rosenfeld

Remove Duplicate Numbers in One Cell
 
On Sat, 16 Jan 2010 03:42:16 -0800 (PST), Lucky wrote:

My worksheet has one column in which each cell has one and two-digit
numbers separated by spaces, such as

1 18 22 5 1 31 8 11 6 1

I want to remove all the duplicate numbers in each cell, leaving only
one instance of each number in there. (1 and 11 are different
numbers, of course). I've tried a few solutions, but they're clunky
and inelegant and take a long time to do these thousands of cells.

Anyone have a better way?
Thanks,
Lucky


Here's a short Macro that will do that.

As written, it will return the value in the adjacent column, for
troubleshooting. But if it is what you want, you can change that easily in the
code.

===========================
Option Explicit
Sub RemoveDups()
Dim c As Range
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "(\b\d+\b)(?=.*?\1)"
For Each c In Selection
'when debugged, change c.Offset(..).value to
'c.value
c.Offset(0, 1).Value = _
WorksheetFunction.Trim(re.Replace(c.Text, " "))
Next c
End Sub
============================
--ron

Gary''s Student

Remove Duplicate Numbers in One Cell
 
Select a cell and:

Sub uniqueification()
Dim coll As Collection
Dim s As String
Set coll = New Collection

ary = Split(Selection.Value, " ")
ub = UBound(ary)
lb = LBound(ary)
If ub = 0 Then Exit Sub

On Error Resume Next
For i = lb To ub
coll.Add ary(i), CStr(ary(i))
Next

For i = 1 To coll.Count
If i = 1 Then
s = coll(1)
Else
s = s & " " & coll(i)
End If
Next
Selection.Value = s
End Sub

You can, of course, use a loop for more cells.
--
Gary''s Student - gsnu200909


"Lucky" wrote:

My worksheet has one column in which each cell has one and two-digit
numbers separated by spaces, such as

1 18 22 5 1 31 8 11 6 1

I want to remove all the duplicate numbers in each cell, leaving only
one instance of each number in there. (1 and 11 are different
numbers, of course). I've tried a few solutions, but they're clunky
and inelegant and take a long time to do these thousands of cells.

Anyone have a better way?
Thanks,
Lucky
.


Lucky[_6_]

Remove Duplicate Numbers in One Cell
 
Many thanks to everyone. I ended up using Gary"s procedure,
converting it into a function. Now I'll just stick the function in my
module, send it the contents of each cell in the column, and replace
the contents with the results of the function. Works great! I want to
experiment with all 3 solutions because they all contain interesting
concepts that are new to me. I really appreciate the help.
Lucky


All times are GMT +1. The time now is 05:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com