Userform Textbox Date and Time (Elapsed Time) Formatting Question

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,632
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code triggered when a user selects an item form a listbox (lbx_collections). Once clicked, code is used to define values to populate other controls on the userform by cross referencing same data on a worksheet (ws_dump).

Rich (BB code):
Private Sub lbx_collections_Click()
   Stop
   
    Dim tsel As String 'selected value
    Dim tselix As Long 'selected index
    Dim j As Long, drow As Long
    Dim tseltitle As String, tserlcoll As String, tseldate As Date, tseldur As Date
    Dim gt As Boolean
    Dim txttseldate as string
    
    If Not mbevents Then Exit Sub
    tsel = lbx_collections.Value
    tselix = lbx_collections.ListIndex
    'MsgBox tsel & " (" & tselix & ")"
    tseltitle = tsel '
    tselcoll = lbx_collections.List(tselix, 1) 'collected?
    drow = WorksheetFunction.Match(tseltitle, ws_dump.Columns("C"), 0)
    'date
    tseldate = ws_dump.Range("D" & drow)
    tseldy = Day(tseldate)
    tselmn = MonthName(Month(tseldate), True)
    tselyr = Year(tseldate)
    txttseldate = tseldy & "-" & tselmn & "-" & tselyr
    
    tseldur = ws_dump.Range("E" & drow)
    tselres = ws_dump.Range("F" & drow)
    If ws_dump.Range("B" & drow) <> "" Then
        gt = True
        'get list of participants, create a list, and update lbx_smodels
        With ws_dump
            ml_lstrow = .Cells(.Rows.count, "AA").End(xlUp).Row
            .Range("AA3:AA" & ml_lstrow).Sort key1:=.Range("L2"), Order1:=xlAscending, Header:=xlNo
            .Range("AA3:AA" & ml_lstrow).Name = "modlist2"
        End With
    End If
    
    mbevents = False
    Me.tbx_title.Value = tseltitle
    If tselcoll = "OK" Then
        Me.chkbx_collected.Value = True
    Else
        Me.chkbx_collected.Value = False
    End If
    Me.tbx_date.Value = txtseldate
    Me.tbx_dur.Value = tseldur
    Me.cbx_res.Value = tselres
    If gt = True Then
        Me.chkbx_group.Value = True
        Me.lbx_smodel.List = Application.WorksheetFunction.Transpose(ThisWorkbook.names("modlist2").RefersToRange)
    Else
        Me.chkbx_group.Value = False
        Me.lbx_smodel.List = Application.WorksheetFunction.Transpose(ThisWorkbook.names("modlist").RefersToRange)
    End If
    mbevents = True
End Sub

The lines purple extract a time duration (elapsed time) from column E formatted as hh:mm:ss AM/PM (which may not be the best way to display elapsed time). So cell E2 for example has a value of 12:06:33 AM, but displays as 0:06:33 to represent a duration of 6 minutes and 33 seconds.

The code captures the data correctly, however, the value is not displaying in the userform control correctly.

The duration gets populated into userform textbox "tbx_dur" from data extracted from cell (see above), with tseldur = 12:06:33 AM as 12:06:33 AM. I want this textbox to display the value as 0:06:33 (h:mm:ss). What must I do to make this happen?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Have you tried .Text property?
e.g
Code:
    Me.tbx_date.Value = ws_dump.Range("D" & drow).Text
    Me.tbx_dur.Value = ws_dump.Range("E" & drow).Text
 
Upvote 0
Solution
Hi
As you have declared variable tseldur As Date try changing this line

VBA Code:
Me.tbx_dur.Value = tseldur

to this

VBA Code:
Me.tbx_dur.Value = Format(tseldur, "hh:mm:ss")

and see if resolves your issue.

Dave
 
Upvote 0

Forum statistics

Threads
1,225,168
Messages
6,183,307
Members
453,155
Latest member
joncaxddd

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