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.
 
@Rolsu
I amended your code in post #8:
VBA Code:
Private Sub ComboBox2_DropButtonClick()

    Dim dict As Object, item As Variant
    Dim cel, rng As Range
    Dim lr As Long
    Dim va
        lr = Worksheets("Time").Cells(Rows.Count, "A").End(xlUp).Row
        Set rng = Worksheets("Time").Range("A2:A" & lr)  'data start at row 2
        Set dict = CreateObject("scripting.dictionary")

                rng.Sort Key1:=rng.Cells(1), Order1:=xlAscending, Header:=xlNo, _
                OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal     ' sort the range
                va = rng.Value
                    For Each cel In va
                        dict(Format(cel, "yyyy")) = Empty
                    Next
                combobox2.List = dict.keys
End Sub
 
Upvote 0
Solution

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
@Rolsu
I amended your code in post #8:
VBA Code:
Private Sub ComboBox2_DropButtonClick()

    Dim dict As Object, item As Variant
    Dim cel, rng As Range
    Dim lr As Long
    Dim va
        lr = Worksheets("Time").Cells(Rows.Count, "A").End(xlUp).Row
        Set rng = Worksheets("Time").Range("A2:A" & lr)  'data start at row 2
        Set dict = CreateObject("scripting.dictionary")

                rng.Sort Key1:=rng.Cells(1), Order1:=xlAscending, Header:=xlNo, _
                OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal     ' sort the range
                va = rng.Value
                    For Each cel In va
                        dict(Format(cel, "yyyy")) = Empty
                    Next
                combobox2.List = dict.keys
End Sub
Hi Akuini!
It works!
One small thing I'd like to ask about this. This code works only when I change the date format in my reference list to dd/mm/yyyy. Is any chance to make this work with the format dd.mm.yyyy? If not than it's ok too. I ask this because the general format in my region in the last one.
Thank you.
 
Upvote 0
This code works only when I change the date format in my reference list to dd/mm/yyyy. Is any chance to make this work with the format dd.mm.yyyy?
I don't understand why it won't work with format dd.mm.yyyy.
What actually happened when you run the code with data format dd.mm.yyyy?
Could you post a sample data?
 
Upvote 0
I don't understand why it won't work with format dd.mm.yyyy.
What actually happened when you run the code with data format dd.mm.yyyy?
Could you post a sample data?
If I use the original format (24.08.2023) you can see the result in the picture. The sorting function works fine, because one date appears only once but the whole date, not just the year.
 

Attachments

  • Picture.jpg
    Picture.jpg
    51.2 KB · Views: 7
Upvote 0
If I use the original format (24.08.2023) you can see the result in the picture. The sorting function works fine, because one date appears only once but the whole date, not just the year.
Maybe the date in the sheet isn't really a date but text.
Test it like this:
Book1
AB
101.02.20202020
201.02.2020#VALUE!
3
Sheet4
Cell Formulas
RangeFormula
B1:B2B1=YEAR(A1)

I entered data in A1 as date but in A2 as a text, so Year function returns error on A2.
Try YEAR function on your data, see what the result is.
 
Upvote 0
Maybe the date in the sheet isn't really a date but text.
Test it like this:
Book1
AB
101.02.20202020
201.02.2020#VALUE!
3
Sheet4
Cell Formulas
RangeFormula
B1:B2B1=YEAR(A1)

I entered data in A1 as date but in A2 as a text, so Year function returns error on A2.
Try YEAR function on your data, see what the result is.
The format is official date, so the problem was not that I think.
Meanwhile I figured it out. The data source comes from a userform textbox, where I can enter a date, or accept the auto one (that tbox refers to a cell whit the actual date). I formated the tbox value to "dd/mm/yyyy". The result is almost the same with a small difference. After clicking the save button excel stores the date in this format: 24. 08. 2023, instead of this: 24.08.2023. There's a space after the dot in the new format. And now your code works properly.
Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,704
Messages
6,173,984
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