简体   繁体   中英

Listbox excel vba

I have two columns a sheet. 'Year' and 'Make'. I am trying to design a userform with two combobox for year and make. Every time I run the macro, I want the combobox 'year' to have one instance of every year from the 'year' column. say ther are 10 instances of 2014, 20 instances of 2015 and 30 od 2016. I need to have one instance of all three years. The number of unique years may vary. Is there a way I could do this?

If I use data validation, I get all thee instances. I just want it to appear once. Please help me out. Either data validation or the combobox method is fine for me. I just need to have a place where we can select one 'year' from the available years.

This is my idea of using a Pivot Table .
Select your Year column and make a table out of it.
Then summarize it using Pivot Table . Place it in Report Filter Field and you have your dropdown.

在此处输入图片说明

Another none VBA way is to use a helper column where it only outputs the unique values as discussed HERE .

Using this formula: (credit's to Oscar)

=IFERROR(INDEX(Table1[Year],MATCH(MIN(IF(COUNTIF($C$1:C1,Table1[Year])=0,1,MAX((COUNTIF(Table1[Year],"<"&Table1[Year])+1)*2))*(COUNTIF(Table1[Year],"<"&Table1[Year])+1)),COUNTIF(Table1[Year],"<"&Table1[Year])+1,0)),"")

And entering it using Ctrl + Shift + Enter .
Take note that we used the table we just created.

在此处输入图片说明

After that, create a named range out of it.

在此处输入图片说明

And finally, you create the validation:

在此处输入图片说明

If you still need VBA, yes it can be done in various ways.
But before that, gauge if any of the two ways above will work for you. HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM