Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

Question

Has anyone found a problem with date handling in Excel 2010

Aug 22, 2014 2:01AM PDT

I have generated dates for the first fourteen days of August to demonstrate the error. The dates are created using DateSerial function varying the day part; these are loaded into a combobox (and displayed via Msgbox to check); Msgbox displays the dates in dd/mm/yyyy format as expected but when the dates are displayed in the combobox they appear to be in mm/dd/yyyy format; selecting the first twelve entries I get the 8th of each month but selecting the last two entries I get the 13th or 14th of August! Needless to say, access to Microsoft has produced no one who can help as this is a coding problem or not to do with Office! I can supply demo code if required.

Thanks.

Discussion is locked

- Collapse -
Answer
Can be true.
Aug 22, 2014 2:12AM PDT
- Collapse -
Is there any hope?
Aug 22, 2014 5:45AM PDT

I take it that there is no "fix" to this problem! Apart, that is, to coding my own handler, but I am wary that the results I use will be reliable. I used the following code with a blank worksheet and a userform with one combobox (called combobox1) to show the error.

Private Sub ComboBox1_Change()

MsgBox "ComboBox1.Value " & TypeName(ComboBox1.Value) & " " & ComboBox1.Value & vbCr & _
"ComboBox1.Text " & TypeName(ComboBox1.Text) & " " & ComboBox1.Text & vbCr & _
FormatDateTime(ComboBox1.Text, vbShortDate) & " " & FormatDateTime(ComboBox1.Text, vbLongDate) & " " & CStr(ComboBox1.Text)

Sheet1.Range("A" & Day(ComboBox1.Text)).Value = (ComboBox1.Text)

End Sub

Private Sub UserForm_Initialize()
Dim d1 As Date

For dd = 1 To 14
ComboBox1.AddItem DateSerial(2014, 8, dd)
MsgBox DateSerial(2014, 8, dd) & vbCr & CStr(DateSerial(2014, 8, dd))
Next dd

End Sub

- Collapse -
Looking at your code
Aug 22, 2014 12:07PM PDT

Looking at your code, I'm wondering what the point of the dateserial function is. You're hard coding the month and year, so why not just have something like

combobox1.additem dd & "/08/2014"

Why waste time on a function that isn't even working correctly when you're essentially feeding it the data you want anyway? A simple switch case routine could handle whether the month has 30 or 31 days (or 28 even 29). Nested for loops to handle the year and month or you could just use the end of the year as a convenient excuse to push a new update to the program. Tiny bit of job security.

- Collapse -
Not so helpful....
Aug 23, 2014 9:38AM PDT

Thanks for the suggestion but if I had employed this method the simple demonstration code would not reflect the functionality of the original code which calculates new dates by varying the Year, Month and Day parts by varying amounts. Accuracy to the day is of import in the original code.

- Collapse -
If you know
Aug 23, 2014 11:13AM PDT

If you know you're consistently getting mm/dd/yyyy you could just break it up and reassemble it. Something like:

newdate = mid(4, 2, olddate) & "/" & left(2, olddate) & "/" & right(4, olddate)

My syntax might be somewhat off, but it should give you the basic idea. You know the format of the date you're getting, so you can break it into substrings and then reassemble it in the format you want. If you're not positive you're always going to have two digits you could probably break it into substrings based on the / character. It's a lot of work that only you will even know exists, but the alternative is you will just have to live with the fact that the function wasn't internationalized very well.

- Collapse -
Have to go with no.
Aug 23, 2014 1:29AM PDT

Unless you simplify it as noted by Jimmy G, all is working according to plan. That is, if regional settings are such then the formatting SHALL change. This one really grinds on those that haven't done international apps.
Bob

- Collapse -
Re: combobox
Aug 23, 2014 1:50AM PDT

- In the properties of the combobox, what is the format?
- What are the windows settings (regional) for date format?

Kees