Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default 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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default 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
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default 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
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default 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
.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default 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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
To remove numbers in a cell with out using the macro Vital_ar Excel Worksheet Functions 2 October 27th 09 01:45 PM
Cell to red flag duplicate numbers Bill Excel Discussion (Misc queries) 1 September 16th 07 11:14 PM
How can I remove 4 of 9 numbers from a cell for the whole column? RISXRAY Excel Discussion (Misc queries) 5 September 14th 06 05:33 PM
How do you remove duplicate phone numbers in an Excel spreadsheet Bridgette Excel Discussion (Misc queries) 1 May 25th 06 08:38 PM
remove text from cell containing numbers Tim Excel Discussion (Misc queries) 2 March 22nd 06 02:30 PM


All times are GMT +1. The time now is 12:08 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"