Vba combobox- date format

RockEd

Board Regular
Joined
Aug 13, 2021
Messages
80
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a combobox that links to a named range on my sheet that has a list of dates on it. The named range is called "year2".

The named range automatically updates when a new year starts if you need to know.

However I have tried a few tricks from google but cannot get the format right. All I want to show is the year in YYYY format (e.g. 2022).

However, this code only populates it with "1905"

VBA Code:
Private Sub combo_year_fees_Change()

combo_year_fees.Value = Format(combo_year_fees.Value, "yyyy")

End Sub

and this code populates it with the correct date but in d/m/yyyy format:

VBA Code:
Private Sub combo_year_fees_Change()
Dim myRowSource As String
myRowSource = Format(myRowSource, "yyyy")

End Sub

It is populated from the intialize sub like so:

VBA Code:
Me.combo_year_fees.List = shDateTimes.Range("Year2").Value

I have left RowSource and ControlSource blank at the moment.
Thanks for your help in advance!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
on my sheet that has a list of dates on it
If you have dates and want only the year. You must extract the year from each date.

I don't understand why you have it in the change event, if you are going to load the combo with all the years of the dates, you should do it in the Activate event, for example:

VBA Code:
Private Sub UserForm_Activate()
  Dim dic   As Object
  Dim c     As Range
  
  Set dic = CreateObject("Scripting.Dictionary")
  'only unique years
  For Each c In Range("year2")
    dic(Format(c.Value, "yyyy")) = Empty
  Next
  combo_year_fees.List = dic.keys
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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