VBA to return only unique values from a range of comma separated words on multiple rows.

Ananthak275

Board Regular
Joined
Aug 22, 2020
Messages
128
Office Version
  1. 2013
Platform
  1. Windows
  2. MacOS
Hi everyone,

I have the following rows of texts. My data starts on Row 2 (since there is a header). how do i get the unique values? and them pasted below the last row of that column. For example, CityName (Column A) starts in A2 and ends in A7, so i want the unique values to be pasted start on A8. While StateName starts in B2 and ends in B5, so i would want the unique states starting in b6?


1CityNameStateName
2Dallas. Houston, PhiladelphiaCalifornia, Texas
3
Chicago
Pennsylvania, California, Texas
4
Dallas. Houston, Philadelphia
Arizona, New York
5Houston, Philadelphia, AustinNew York
6Austin, New York City, Los Angeles
7San Jose
8
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
How about
VBA Code:
Sub Ananthak()
   Dim Cl As Range
   Dim Ele As Variant
   
   With CreateObject("scripting.dictionary")
      .CompareMode = 1
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         For Each Ele In Split(Cl.Value, ",")
            .Item(Trim(Ele)) = Empty
         Next Ele
      Next Cl
      Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(.Count).Value = Application.Transpose(.Keys)
   End With
End Sub
And the same for col B, just change the ranges
 
Upvote 0
How about
VBA Code:
Sub Ananthak()
   Dim Cl As Range
   Dim Ele As Variant
  
   With CreateObject("scripting.dictionary")
      .CompareMode = 1
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         For Each Ele In Split(Cl.Value, ",")
            .Item(Trim(Ele)) = Empty
         Next Ele
      Next Cl
      Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(.Count).Value = Application.Transpose(.Keys)
   End With
End Sub
And the same for col B, just change the ranges
Worked perfectly, thanks! is there a way to make the range unique? so it does this for all the columns that contain values?
 
Upvote 0
The range is unique, it's only looking at col A
 
Upvote 0
The range is unique, it's only looking at col A
Sorry I meant dynamic. Instead of selecting col A, b, c, etc every time, is it possible to make it all the run the code on all the columns that contain value?
 
Upvote 0
How about
VBA Code:
Sub Ananthak()
   Dim Cl As Range, Col As Range
   Dim Ele As Variant
   
   With CreateObject("scripting.dictionary")
      .CompareMode = 1
      For Each Col In Range("A1", Cells(1, Columns.Count).End(xlToLeft))
         For Each Cl In Range(Col.Offset(1), Col.Offset(Rows.Count - 1).End(xlUp))
            For Each Ele In Split(Cl.Value, ",")
               .Item(Trim(Ele)) = Empty
            Next Ele
         Next Cl
         Col.Offset(Rows.Count - 1).End(xlUp).Offset(1).Resize(.Count).Value = Application.Transpose(.Keys)
         .removeall
      Next Col
   End With
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0
Fluff sorry i just realized one thing. If you re-run the code, it keeps duplicating the split values. Is there a way to only duplicate the values that dont exist?
 
Upvote 0
Not really, because there is no way of knowing which values to add.
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

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