Listbox dates not formatted correctly

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,291
Office Version
2016
Platform
Windows
I have a strange behaviour occurring whilst trying to load a Listbox on a Userform.

I have the following code which populates the Listbox with data dependant on the value of column E:

Code:
Private Sub UserForm_Initialize()Dim Rng As Range, Dn As Range, c As Long, Ray()
Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
For Each Dn In Rng
    If Range("E" & Dn.Row) = vbNullString Then
        c = c + 1
        ReDim Preserve Ray(1 To 4, 1 To c)
        Ray(1, c) = Dn.Value
        Ray(2, c) = Dn.Offset(, 1).Value
        Ray(3, c) = Dn.Offset(, 2).Value
        Ray(4, c) = Dn.Offset(, 3).Value
    End If
Next Dn
With Me.ListBox1
    .Clear
    .ColumnCount = 4
    .ColumnWidths = "60;120,75,50"
    .List = Application.Transpose(Ray)
End With
End Sub
If I load the userform from within the VBE the dates are formatted correctly, (dd/mm/yyyy hh:mm - these are in column C on the sheet). However, if I load the userform from a custom ribbon command which simply says "Userform1.show" then it loads the dates as "mm/dd/yyyy hh:mm:ss".

Does anyone know why this may be the case?
 

Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,189
Office Version
2007
Platform
Windows
Maybe your excel version, I tried it and anyway the format is "dd / mm / yyyy"
You can set it with the following:

Code:
Ray(3, c) = Format(Dn.Offset(, 2).Value, "dd/mm/yyyy hh:mm")
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
10,189
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,089,241
Messages
5,407,105
Members
403,125
Latest member
vbambre

This Week's Hot Topics

Top