Want unique words from a cell having multiple words separated by comma.

mshtuhin

New Member
Joined
Nov 19, 2008
Messages
10
Dear Experts,

I have value like:

A1= India, United States, China, United Kingdom,
B1= India, China, Singapore, Iran, Canada,

I want result in C2=India, United States, China, United Kingdom, Singapore, Iran, Canada,

i.e combine all value but delete the duplicate, here duplicate is India and China.

==========================================================
Have also posted to excelforum.com Below is the link:
http://www.excelforum.com/excel-gen...n-delete-duplicate.html?p=2856123#post2856123

<tbody>
</tbody>
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
try this
Code:
Sub remDup()
Dim dic As Object, cell As Range, temp As Variant
Dim i As Long
Cells(2, 3) = Range("A1") & " " & Range("A2")
Set dic = CreateObject("scripting.dictionary")
With dic
    For Each cell In Range("C1:C" & Cells(Rows.Count, "A").End(xlUp).Row)
        .RemoveAll
        If Len(cell.Value) > 0 Then
            temp = Split(" " & cell.Value, ",")
            For i = 0 To UBound(temp)
                If Not .Exists(temp(i)) Then .Add temp(i), temp(i)
            Next i
            cell.Value = Mid(Join(.Keys, ","), 2)
        End If
    Next cell
End With
       
End Sub
 
Upvote 0
Hi,

Thanks for your code, however please note, your macro returns result= Country group India, United States, China, United Kingdom

but my expected result should be: India, United States, China, United Kingdom, Singapore, Iran, Canada,

i.e combine values from A and B column to C and then keep only the unique countries separated by comma.
 
Upvote 0
here is what I get from running the macro


Excel 2003
ABC
1India, United States, China, United Kingdom,
2India, China, Singapore, Iran, Canada,India, United States, China, United Kingdom, Singapore, Iran, Canada,
Sheet2
 
Upvote 0
you are perfect, in my sheet I had my column header which caused the problem, Now I have edited the code you given as per my requirement.

Many many thanks for your support :)

brgds
Tuhin
 
Upvote 0
Hi,

Further to above, please note, i am getting few anomalies. below is an eg.

A1 = REPUBLIC OF KOREA, UNITED REPUBLIC OF TANZANIA, VIETNAM, UNITED STATES, UNITED ARAB EMIRATES, THAILAND, TAIWAN, SPAIN, SINGAPORE, PEOPLES REPUBLIC OF CHINA, PAKISTAN, MOROCCO, MALAYSIA, MADAGASCAR, JORDAN, ***AN, INDONESIA, INDIA, HONG KONG, GERMANY, DENMARK, BELGIUM,

A2 = REPUBLIC OF KOREA, UNITED REPUBLIC OF TANZANIA, VIETNAM, UNITED STATES, UNITED ARAB EMIRATES, THAILAND, TAIWAN, SPAIN, SINGAPORE, PEOPLES REPUBLIC OF CHINA, PAKISTAN, MOROCCO, MALAYSIA, MADAGASCAR, JORDAN, ***AN, INDONESIA, INDIA, HONG KONG, GERMANY, DENMARK, BELGIUM,

Running your macro, I am getting below result in C2

REPUBLIC OF KOREA, UNITED REPUBLIC OF TANZANIA, VIETNAM, UNITED STATES, UNITED ARAB EMIRATES, THAILAND, TAIWAN, SPAIN, SINGAPORE, PEOPLES REPUBLIC OF CHINA, PAKISTAN, MOROCCO, MALAYSIA, MADAGASCAR, JORDAN, ***AN, INDONESIA, INDIA, HONG KONG, GERMANY, DENMARK, BELGIUM, REPUBLIC OF KOREA,

i.e. REPUBLIC OF KOREA appears twice, could you please spare some time for me to check the macro.

thx
Tuhin
 
Last edited:
Upvote 0
I tested TexasLynn's code against your supplied strings and returned:

REPUBLIC OF KOREA, UNITED REPUBLIC OF TANZANIA, VIETNAM, UNITED STATES, UNITED ARAB EMIRATES, THAILAND, TAIWAN, SPAIN, SINGAPORE, PEOPLES REPUBLIC OF CHINA, PAKISTAN, MOROCCO, MALAYSIA, MADAGASCAR, JORDAN, ***AN, INDONESIA, INDIA, HONG KONG, GERMANY, DENMARK, BELGIUM,
 
Upvote 0
Very close to Lynn's; I just chose to skip the IF test while looping. As a UDF:


Excel 2000
AB
1REPUBLIC OF KOREA, UNITED REPUBLIC OF TANZANIA, VIETNAM, UNITED STATES, UNITED ARAB EMIRATES, THAILAND, TAIWAN, SPAIN, SINGAPORE, PEOPLES REPUBLIC OF CHINA, PAKISTAN, MOROCCO, MALAYSIA, MADAGASCAR, JORDAN, ***AN, INDONESIA, INDIA, HONG KONG, GERMANY, DENMARK, BELGIUM,REPUBLIC OF KOREA, UNITED REPUBLIC OF TANZANIA, VIETNAM, UNITED STATES, UNITED ARAB EMIRATES, THAILAND, TAIWAN, SPAIN, SINGAPORE, PEOPLES REPUBLIC OF CHINA, PAKISTAN, MOROCCO, MALAYSIA, MADAGASCAR, JORDAN, ***AN, INDONESIA, INDIA, HONG KONG, GERMANY, DENMARK, BELGIUM, CANADA, MEXICO
2REPUBLIC OF KOREA, UNITED REPUBLIC OF TANZANIA, VIETNAM, UNITED STATES, UNITED ARAB EMIRATES, THAILAND, TAIWAN, SPAIN, SINGAPORE, PEOPLES REPUBLIC OF CHINA, PAKISTAN, MOROCCO, MALAYSIA, MADAGASCAR, JORDAN, ***AN, INDONESIA, INDIA, HONG KONG, GERMANY, DENMARK, BELGIUM,
3CANADACANADA
4CANADA, MEXICOCANADA, MEXICO
5MEXICO,MEXICO
6No Vals
Sheet2
Cell Formulas
RangeFormula
B1=UNIQUES(A1:A5)
B3=UNIQUES(A3)
B4=UNIQUES(A4)
B5=UNIQUES(A5)
B6=UNIQUES(A6)


In a Standard Module:

Rich (BB code):
Option Explicit
    
Function UNIQUES(ByVal CellRange As Range) As String
Static DIC      As Object 'Dictionary
Dim Cell        As Range
Dim CellString  As String
Dim aryStrings  As Variant
Dim n           As Long
    
    If DIC Is Nothing Then Set DIC = CreateObject("Scripting.Dictionary")
    
    DIC.RemoveAll
    For Each Cell In CellRange
        CellString = Cell.Value
        If InStr(1, CellString, ",") > 0 Then
            aryStrings = Split(CellString, ",")
            For n = LBound(aryStrings, 1) To UBound(aryStrings, 1)
                DIC.Item(Trim(aryStrings(n))) = Empty
            Next
        Else
            DIC.Item(Trim(CellString)) = Empty
        End If
    Next
    
    If DIC.Exists(vbNullString) Then DIC.Remove (vbNullString)
    
    If DIC.Count > 1 Then
        UNIQUES = Join(DIC.Keys, ", ")
    ElseIf DIC.Count = 1 Then
        '// To get past an anomaly of late-bound DIC, as we cannot use DIC.Keys(0)  //
        aryStrings = DIC.Keys
        UNIQUES = aryStrings(0)
    Else
        UNIQUES = "No Vals"
    End If
End Function

Hope that helps,

Mark
 
Upvote 0
Hi Mark,

This is simply excellent ! You are absolutely right, it is working exactly same as i expected. I tried with different combination in different file, its wonderful. I really like function code. Thank you soooo much :)

Tuhin
 
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,880
Members
449,477
Latest member
panjongshing

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