PDA

View Full Version : Solved: remove the repeated words/numbers in a cell



rafi_07max
01-23-2011, 07:54 AM
I have some contents in cell B1 and cell C1.
For e.g. the content in cell B1 is,
DAT8, G8, D4, A8, NQ8, "C7", ground, G7, 5, P14

And the content in cell C1 is
DAT8, D4, ground, C8, "C7", E7, 5, F4, F3, E8

If we compare the two contents we can see that some values can be found in both cells.
For e.g. DAT8, D4, ground, “C7” and 5

So what I want to do is to remove all these repeated contents from cell C1.

After running the macro successfully the content in cell C1 should be
C8, E7, F4, F3, E8

And the content in cell B1 should remains as how it was initially,
DAT8, G8, D4, A8, NQ8, "C7", ground, G7, 5, P14

Zack Barresse
01-23-2011, 10:54 AM
Hi there,

Maybe somewhat crude, and not dynamic (but you didn't specify much else), you could use something like this ...

Option Explicit

Const sDelim As String = ", "

Sub RemoveDupData()
Dim WS As Worksheet, rLook As Range, rTake As Range
Dim aData() As String, aCheck() As String
Dim i As Long, j As Long, sTemp As String
Dim bFound As Boolean
Set WS = ActiveSheet
Set rLook = WS.Range("B1")
Set rTake = WS.Range("C1")
aData = Split(rLook.Value, sDelim)
aCheck = Split(rTake.Value, sDelim)
For j = LBound(aCheck) To UBound(aCheck)
bFound = False
For i = LBound(aData) To UBound(aData)
If aCheck(j) = aData(i) Then
bFound = True
Exit For
End If
Next i
If bFound = False Then sTemp = sTemp & aCheck(j) & sDelim
Next j
sTemp = Left(sTemp, Len(sTemp) - 2)
rTake.Value = sTemp
End Sub

HTH

rafi_07max
01-23-2011, 11:16 PM
Thanks zack for your help and time. It works :)