Concatenate Unique Values - Minor VBA issue

Dan5977

Board Regular
Joined
Aug 7, 2008
Messages
111
Office Version
  1. 2010
Hi all,

I am having a minor issue with a function for concatenating a range into a comma seperated list. The function is as follows:

Code:
Function ConcatUniq(xRg As Range, xChar As String) As String
    Dim xCell As Range
    Dim xDic As Object
    Set xDic = CreateObject("Scripting.Dictionary")
    For Each xCell In xRg
        xDic(xCell.Value) = Empty
    Next
    ConcatUniq = Join$(xDic.Keys, xChar)
    Set xDic = Nothing
End Function

I then enter =ConcatUniq(finish,",") into the cell where I want the comma seperated list (finish is a named range).

This works really well apart from one minor issue. I am getting an extra comma after the first item in the list.
So it would say for example 'Black,,Yellow,Orange' instead of 'Black,Yellow,Orange'

As always, any help would be appreciated.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
See if this works:

Code:
Set xDic = CreateObject("Scripting.Dictionary")
For Each xCell In xRg
    s = Trim(xCell.Value)
    If Len(s) > 0 Then xDic(s) = xDic(s) + 1
Next xCell
ConcatUniq = Join$(xDic.keys, xChar)
 
Upvote 0
see if this does what you want
- adjusted to ignore EMPTY cells

Code:
Function ConcatUniq(xRg As Range, xChar As String) As String
    Dim xCell As Range
    Dim xDic As Object
    Set xDic = CreateObject("Scripting.Dictionary")
    For Each xCell In xRg
        [COLOR=#b22222]If xCell <> "" Then xDic(xCell.Value) = Empty[/COLOR]
    Next
    ConcatUniq = Join$(xDic.Keys, xChar)
    Set xDic = Nothing
End Function


If you have Excel 365 (or possibly Excel 2019 ??) function TEXJOIN returns what you want
(test the function - it either returns #NAME? error or the correct answer! )
delimiter = ",", ignore empty cells = TRUE, Concatentate range F2:K2
=TEXTJOIN(",",TRUE,F2:K2)
 
Last edited:
Upvote 0
Textjoin wont do unique used like that which i guess is required going by the function name :)
 
Upvote 0
Both of those solutions work. Thank you very much :)

Unfortunately we are still stuck on Excel 2010! If we had Office365 I would probably have used the new UNIQUE function to create a unique list to refer to. Until then this works great thanks.
 
Upvote 0

Forum statistics

Threads
1,223,262
Messages
6,171,080
Members
452,377
Latest member
bradfordsam

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