VBA to create ComboBox List from values in a column

chaddres

Board Regular
Joined
Jun 14, 2014
Messages
143
Office Version
  1. 365
Platform
  1. Windows
I would like to populate my ComboBox with a list of years that appear in Column I. The issue I have is that column I has multiple recurrences of each year and I only want to add each year one time. So, there might be twenty recurrences of 2004, but I only want to add it one time. Then, there might be ten recurrences of 2005. And, so on.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi chaddres,

you could copy the unique year values to another sheet and then use this as the rowsource for your ComboBox... assuming your ComboBox is on a userform (UserForm1) and the sheet with the years on is "1" and sheet "2" is empty you can give this code a try.

Code:
Sub UniqueComboBox()

Dim LastRw As Long
Dim LastUnqRw As Long

LastRw = Sheets(1).Range("I" & Rows.Count).End(xlUp).Row 'count of used cells in column I

Sheets(1).Range("I1:I" & LastRw).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("2").Range("A1"), Unique:=True 'copy unique values to sheet 2

LastUnqRw = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row 'count of unique values

UserForm1.ComboBox1.RowSource = Sheets("2").Range("A2:A" & LastUnqRw) 'A2 (assuming A1 is the header) to end of unique values

End Sub

Hope this helps,
Cheers,
Alan.
 
Upvote 0
Could I do a sort by year and then do something like "if b2 <> b1, then add it to the combo box"? Then, I could move down the column (which would be sorted by year) and only add new values. Thoughts?
 
Upvote 0
yes good idea... add this to the userform code

Code:
Private Sub UserForm_Initialize()

LastRw = Range("I" & Rows.Count).End(xlUp).Row

For i = 1 To LastRw 

    If Cells(i, "I").Value <> Cells(i + 1, "I").Value Then 
    
    p = Cells(i, "I").Value 

    UserForm1.ComboBox1.AddItem p
    
    End If
    
Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

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