combobox populated with dates

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
Is there a way to populate a userform combobox with the dates within a month or a date range?

Example1: user wants a list of all the dates in the month of February 2012. Combobox would be filled with the date range 2/1/12 - 2/29/12, one list item for each day.

Example2: user wants a list of all the dates within the range 2/1/12 - 3/31/12. Combobox would be filled with the date range 2/1/12 - 3/31/12, one list item for each day.

If anyone knows of a way to do something like one of the examples above I would love to know how. A way to do example1 would be great, but my project would work better with example2.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
If user enters their date range with beginning date in B1 and ending date in B2, enter these formulas:
In C1: =B1
In C2 and copy down: =IF(C1="","",IF(C1+1>B$2,"",C1+1))
The input range of the format control of the combo box would be C1 thru as far as you want to allow in the combo box.
 
Upvote 0
Assuming that on your userform you have 2 text boxes for date range begin/end (called TextBox1 and TextBox2), a command button (called CommandButton1) and the combobox called Combobox1, this commandbutton_click event should work. I'd advise more meaningful control names, though.

Code:
Private Sub CommandButton1_Click()
    Dim d                               As Date
    If IsDate(Me.TextBox1.Value) And IsDate(Me.TextBox2.Value) Then
        'break comparison to two if statements so we don't have to
        'worry about non-date values throwing an error on a > comparison
        If Me.TextBox2.Value > Me.TextBox1.Value Then
            ComboBox1.Clear
            For d = Me.TextBox1.Value To Me.TextBox2.Value
                ComboBox1.AddItem d
            Next d
            ComboBox1.ListIndex = 0
        Else
            MsgBox ("end date must be after begin date")
        End If
    Else
        MsgBox ("Please enter valid dates")
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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