Choose only the year in the combobox

Rolsu

New Member
Joined
Jul 16, 2019
Messages
27
Office Version
  1. 2021
Platform
  1. Windows
Hi all!

I'd like to do a timetable so I can easily manage my projects and I need help to complete my macro.
I have a combobox where I'd like to select the year. I also have many dates (one can be repetative) in a different sheet in column "A" (with header) that I'd like to use as the source of the CB's list. I know how to refer that list so I can choose the values in the combobox.
My queation is how can I select only the year in the combobox using the list mentioned above? Is it possible to do this only with macro? And it would be nice if one year is included only ones.
Thank you.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi all!

I'd like to do a timetable so I can easily manage my projects and I need help to complete my macro.
I have a combobox where I'd like to select the year. I also have many dates (one can be repetative) in a different sheet in column "A" (with header) that I'd like to use as the source of the CB's list. I know how to refer that list so I can choose the values in the combobox.
My queation is how can I select only the year in the combobox using the list mentioned above? Is it possible to do this only with macro? And it would be nice if one year is included only ones.
Thank you.
If I understand correctly, you have a list of dates in a worksheet that is populating a ComboBox. You want only the year displayed in the ComboBox, correct? with/without duplicate years?
 
Upvote 0
Office 2021.

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
Thank for the advise! It is done.
 
Upvote 0
Meanwhile I read a little in the topic and found that this issue can be solved if I create a dictionary.
I found some hints online but I can't make it work.
My code is:
VBA Code:
Private Sub ComboBox2_DropButtonClick()

    Dim dict As Object, item As Variant
    Dim cel, rng As Range
    Dim lr As Long
    
        lr = Cells(Rows.Count, "A").End(xlUp).Row
        
        Set dict = CreateObject("scripting.dictionary")
        Set rng = Worksheets("Time").Range("A1:A" & lr)

                rng.Sort Key1:=rng.Cells(1), Order1:=xlAscending, Header:=xlYes, _
                OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal     ' sort the range

                    For Each cel In rng
                        If Not dict.exists(Format(cel, "yyyy")) Then
                        dict.Add Format(cel, "yyyy"), Format(cel, "yyyy")
                    End If
                    Next

                    For Each item In dict.items
                        ComboBox2.AddItem
                        
                    Next

        Set dict = Nothing
        Set rng = Nothing

End Sub
When I open the drop down list it shows nothing.
Can someone help me with this?
Thank you.
 
Upvote 0
Meanwhile I read a little in the topic and found that this issue can be solved if I create a dictionary.
I found some hints online but I can't make it work.
My code is:
VBA Code:
Private Sub ComboBox2_DropButtonClick()

    Dim dict As Object, item As Variant
    Dim cel, rng As Range
    Dim lr As Long
   
        lr = Cells(Rows.Count, "A").End(xlUp).Row
       
        Set dict = CreateObject("scripting.dictionary")
        Set rng = Worksheets("Time").Range("A1:A" & lr)

                rng.Sort Key1:=rng.Cells(1), Order1:=xlAscending, Header:=xlYes, _
                OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal     ' sort the range

                    For Each cel In rng
                        If Not dict.exists(Format(cel, "yyyy")) Then
                        dict.Add Format(cel, "yyyy"), Format(cel, "yyyy")
                    End If
                    Next

                    For Each item In dict.items
                        ComboBox2.AddItem
                       
                    Next

        Set dict = Nothing
        Set rng = Nothing

End Sub
When I open the drop down list it shows nothing.
Can someone help me with this?
Thank you.
There was a missing "item" element after the Additem, but after added I still not get the desired data...
 
Upvote 0
Hi,
untested but see if this helps you

Place in STANDARD module

VBA Code:
Function GetYears(Optional ByVal sh As Object, Optional ByVal ListColumn As Long = 1) As Variant
    Dim rng         As Range, cell       As Range
    Dim Years()     As Variant, YearDate As Variant
    Dim i           As Long
   
    If sh Is Nothing Then Set sh = ActiveSheet
   
    Set rng = sh.UsedRange.Columns(ListColumn)
   
    ReDim Years(1 To rng.Cells.Count)
    For Each cell In rng.Cells
        YearDate = cell.Value
        If IsDate(YearDate) Then
            If IsError(Application.Match(Year(YearDate), Years, 0)) Then
                i = i + 1: Years(i) = Year(YearDate)
            End If
        End If
    Next cell
    i = IIf(i = 0, 1, i)
    ReDim Preserve Years(1 To i)
   
    GetYears = Years
End Function

Function has two optional arguments where you can specify
- worksheet object (default Activesheet)
- column number (default column 1)

to allow use in other parts of your project if needed

to call

VBA Code:
Private Sub UserForm_Initialize()
  Me.ComboBox1.List = GetYears(Worksheets("Time"),1)
End Sub
I included the arguments for illustration purposes only - change as required.

Dave
 
Upvote 0

Forum statistics

Threads
1,223,703
Messages
6,173,973
Members
452,540
Latest member
haasro02

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