Populate combo box for year from dates in a column

lex_m

Board Regular
Joined
Aug 5, 2003
Messages
155
I know how to populate a combo box like this, where I have years listed in this range like 2005, 2006……..

Me.CBYear.List = Sheet2.Range("G1:G10").Value

I have dates in column A, Is there anyway to populate it using the dates that are in A1 : A??, the last row with a date in it, for the first year in the column to whatever year it is now?
So if I had dates starting with 2-25-2005 the combo box would populate with 2005, 2006, 2007 ……. 2011

this is for excel 2003
Thanks
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi there,

How about something like this:

(Remove the space between Button and Click!)

Code:
Private Sub ComboBox1_DropButton Click()
    Dim rngyears As Range, lnglrow As Long, intstartyear As Integer
    Application.EnableEvents = False
    lnglrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    Set rngyears = ActiveSheet.Cells(2, 1).Resize(lnglrow, 1)
    intstartyear = Year(WorksheetFunction.Min(rngyears))
    arryears = Array( _
                intstartyear, _
                intstartyear + 1, _
                intstartyear + 2, _
                intstartyear + 3, _
                intstartyear + 4, _
                intstartyear + 5)
    ComboBox1.List = arryears
    Set rngyears = Nothing
    Application.EnableEvents = True
End Sub

This checks the data for the minimum year, loads an array with six years starting with the earliest year, and updates the combobox every time the drop-button is clicked.
 
Last edited:
Upvote 0
... or (now I've read your post again) ...

(Again, remove the space between Button and Click)

Code:
Private Sub ComboBox1_DropButton Click()
    Dim rngyears As Range, lnglrow As Long, intstartyear As Integer, intendyear As Integer
    Application.EnableEvents = False
    lnglrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    Set rngyears = ActiveSheet.Cells(2, 1).Resize(lnglrow, 1)
    intstartyear = Year(WorksheetFunction.Min(rngyears))
    intendyear = Year(Date)
    j = 0
    ReDim arryears(intendyear - intstartyear)
    For i = intstartyear To intendyear
        arryears(j) = i
        j = j + 1
    Next i
    ComboBox1.List = arryears
    Set rngyears = Nothing
    Application.EnableEvents = True
End Sub
 
Upvote 0
Thanks that works, forgot one thing, I am using a change event on the combo box and need it to have All Dates as one of the things in the list, so is there a way to have All Dates to be put in at the top of the box also?
Tried ComboBox1.List ="All Dates" & arryears but That did not do it
 
Upvote 0
You could add the "All years" entry to the start of the array if you want it to appear at the top of the list:

Code:
Private Sub ComboBox1_DropButton Click()
    Dim rngyears As Range, lnglrow As Long, intstartyear As Integer, intendyear As Integer
    Application.EnableEvents = False
    lnglrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    Set rngyears = ActiveSheet.Cells(2, 1).Resize(lnglrow, 1)
    intstartyear = Year(WorksheetFunction.Min(rngyears))
    intendyear = Year(Date)
[COLOR=darkgreen]    j = 1
    ReDim arryears(1)
    arryears(0) = "All years"
    ReDim Preserve arryears(intendyear - intstartyear + 1)[/COLOR]
    For i = intstartyear To intendyear
        arryears(j) = i
        j = j + 1
    Next i
    ComboBox1.List = arryears
    Set rngyears = Nothing
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,256
Members
452,901
Latest member
LisaGo

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