Listbox dates not formatted correctly

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,303
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

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.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
11,709
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
11,709
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,099,072
Messages
5,466,438
Members
406,481
Latest member
Dipak543

This Week's Hot Topics

Top