Listbox to show time in correct format

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,081
I want to display 7 entries from columns A and G of a worksheet (sheet1) in a userform listbox. I am using the following code to populate the listbox and it is working fine except the time (in column B) is in decimal form instead of AM/PM form (.861643518518519 = 8:40 PM). Can someone help me modify this code to include a format function that displays the proper time in the listbox please?
VBA Code:
Private Sub UserForm_Initialize()
Dim ws      As Worksheet
Dim rng     As Range
Dim MyArray
Set ws = Sheets("Sheet1")

Set rng = ws.Range("A1:G" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)

With Me.ListBox1
    .Clear
    .ColumnHeads = False
    .ColumnCount = rng.Columns.Count
     MyArray = rng

    .List = MyArray

    .ColumnWidths = "50;60;70;50;60;60;50"
    .TopIndex = 0
End With

End Sub
 

Some videos you may like

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,961
VBA Code:
With Me.ListBox1
    .Clear
    .ColumnHeads = False
    .ColumnCount = rng.Columns.Count
     MyArray = rng

    .List = MyArray

    For i = 0 to .ListCount -1
        .List(i, 1) = Format(CDate(Val(.List(i, 1))), "h:mm AM/PM")
    Next i

    .ColumnWidths = "50;60;70;50;60;60;50"
    .TopIndex = 0
End With

Side note oddity:
CDate("0.5") returns 12:05 AM , why is that?
CDate(Val("0.5")) returns 12:00 PM as expected
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,526
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
      .List = MyArray
      For i = 0 To .ListCount - 1
         .List(i, 1) = Format(.List(i, 1), "hh:mm Am/pm")
      Next i
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,526
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Glad we could help & thanks for the feedback.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,961
I was running test code and

MsgBox CDate("0.5") returned 12:05:00 AM
MsgBox CDbl(CDate("0.5")) returned 0.00347222222222222

MsgBox CDate(Val("0.5")) returned 12:00:00 PM
MsgBox CDbl(CDate(Val("0.5"))) returned 0.5
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,526
Office Version
  1. 365
Platform
  1. Windows
It appears that CDate sees "0.5" as 00:05:00 for some reason.
Running this
VBA Code:
Debug.Print Format(CDate("0.5"), "h:mm AM/PM"), CDate("0.5")
Debug.Print Format(CDate(0.5), "h:mm AM/PM"), CDate(0.5)
I get
12:05 AM 00:05:00
12:00 PM 12:00:00
 

Watch MrExcel Video

Forum statistics

Threads
1,127,535
Messages
5,625,387
Members
416,099
Latest member
sudarsan23

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
Top