Display unique values in Listbox

dave8

Active Member
Joined
Jul 8, 2007
Messages
275
I have a list on a worksheet:

NameLast NameFirst
Doe John
Smith Betty
Henry Bill
Chen Paul
Doe John
Henry Bill


I don't want to show the duplicate names in the list box. Is there a way to read only the unique names into a listbox?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Are you asking about a data validation dropdown, or a listbox on a user form? If the first, simplest way I've found is to pivot the list and link the data validation to a named range on the pivot. If the second, you can use a scripting.dictionary object to get your unique list (adding if it doesn't exist, do nothing if it does, then read back the array of keys).
 
Upvote 0
The scripting dictionary you mentioned is very new to me and I don't understand how to use it. Can you explain or point me to a source? I appreciate your help.
 
Upvote 0
Google "scripting.dictionary" or take a look at: http://msdn.microsoft.com/en-us/library/x4k5wbx4(v=vs.85).aspx

To use it, either add a reference to the MS Scripting Runtime or use:
Code:
dim x as object
set x = createobject("scripting.dictionary")

and then use code similar to the following (UNTESTED):
Code:
dim names as variant
dim n as long
' process names, adding each new one seen into dictionary
for each aCell in range("A2:A7")
  name = acell.text & " " & acell.offset(0,1).text
  if x.exists(name) then x.add(name, name)
next acell
 
' do something with the names
names = x.keys ' populates with array of strings
for n = lbound(names) to ubound(names)
  ' do something with each names(n)
next n
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,872
Messages
6,122,025
Members
449,060
Latest member
LinusJE

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