Listbox dates not formatted correctly

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. 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?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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")
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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