Listbox to show time in correct format

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
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
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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:
Upvote 0
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
 
Upvote 0
wow thank you this has worked great many thanks for this much appreciated
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,194
Members
448,554
Latest member
Gleisner2

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