Listbox dates not formatted correctly

sharky12345

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

DanteAmor

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

Forum statistics

Threads
1,078,516
Messages
5,340,868
Members
399,396
Latest member
PBE

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top