Userform ComboBox List Date Format

ExcelHobbit

New Member
Joined
Jan 8, 2020
Messages
23
Office Version
  1. 2010
Platform
  1. Windows
I have a named list of dates called Jan_21, showing the following dates in separate cells: 04/01/2021, 11/01/2021, 18/01/2021, 25/01/2021

i then have a userform with a combobox with the list filling from Jan_21. However on the drop down, it shows the US format of mm/dd/yyyy to change it to 01/04/2021, 01/11/2021 etc. i have tried formatting the combobox to the correct date format of dd/mm/yyyy, but the lsit still appears incorrect and then pulls through an incorrect date.

how can i change this?
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You are most likely using the RowSource property of the combo box. I would advise against this, especially because of the problems you are currently experiencing.
Dates in Excel are actually numeric values (converted to an internal date type) while visual representations through controls are texts.
Depending on usage a value of the internal date type is converted back to a numeric value or vice versa. During these conversions, something unexpected can happen, depending on the regional setting.
It is therefore recommended to use the .AddItem method to populate your combo box.

VBA Code:
Private Sub PopulateCombo()
    Dim c As Range
    With Me.ComboBox1
        .Clear
        For Each c In Range("Jan_21")
            .AddItem c.Text
        Next c
    End With
End Sub
 
Upvote 0
Solution
Try this:
VBA Code:
Private Sub UserForm_Initialize()
'Modified  12/8/2021  2:45:59 PM  EST
ComboBox1.Clear
ComboBox1.List = Range("Jan_21").Value
End Sub
 
Upvote 0
You are most likely using the RowSource property of the combo box. I would advise against this, especially because of the problems you are currently experiencing.
Dates in Excel are actually numeric values (converted to an internal date type) while visual representations through controls are texts.
Depending on usage a value of the internal date type is converted back to a numeric value or vice versa. During these conversions, something unexpected can happen, depending on the regional setting.
It is therefore recommended to use the .AddItem method to populate your combo box.

VBA Code:
Private Sub PopulateCombo()
    Dim c As Range
    With Me.ComboBox1
        .Clear
        For Each c In Range("Jan_21")
            .AddItem c.Text
        Next c
    End With
End Sub
this works perfectly, thank you so much. Not only correcting the date issue I had, but also reduced my code by 90%!
 
Upvote 0
You are welcome and thanks for the feedback (y)
 
Upvote 0

Forum statistics

Threads
1,214,547
Messages
6,120,139
Members
448,948
Latest member
spamiki

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