calculating textboxes on userform with custom Formats

Brenton Hombsch

New Member
Joined
May 5, 2019
Messages
6
I have a userform where users can enter times for a team and i am wanting to add the four times together but not sure how to do it.

Each textbox is formatted "0\:00.\00" so that when the user enters the time eg 1500 it shows as 00:15.00 (minutes:seconds.00)

I don't even know where to start in vba mode with this!

Textboxes are named

"Time1"
"Time2"
"Time3"
"Time4"

I am wanting to put the total time of all 4 texboxes into "TotalTime" textbox after pressing a "Calculate" button

Please help
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Here is some code which should do the trick:
Code:
Private Sub Calculate_Click()
    
    Dim Minutes As Long
    Dim Seconds As Double
    Dim Point As Double
    Dim TotalSeconds As Double
    Dim Temp() As String
    Dim sTime As String
    ' ---------------------------------------------------------------------------------------------
    ' Convert first Time TextBox
    ' ---------------------------------------------------------------------------------------------
    sTime = Me.Time1.Text
    Minutes = 0
    Seconds = 0#
    If Not Me.Time1.Text = VBA.Constants.vbNullString Then
        Temp = VBA.Strings.Split(sTime, ":")
        If (UBound(Temp) - LBound(Temp) + 1) = 2 Then
            On Error Resume Next
            Minutes = VBA.Conversion.CLng(Temp(0))
            Seconds = VBA.Conversion.CDbl(Temp(1))
            On Error GoTo 0
            TotalSeconds = TotalSeconds + Minutes * 60 + Seconds
        End If
    End If
    
    ' ---------------------------------------------------------------------------------------------
    ' Convert second Time TextBox
    ' ---------------------------------------------------------------------------------------------
    sTime = Me.Time2.Text
    Minutes = 0
    Seconds = 0#
    If Not Me.Time2.Text = VBA.Constants.vbNullString Then
        Temp = VBA.Strings.Split(sTime, ":")
        If (UBound(Temp) - LBound(Temp) + 1) = 2 Then
            On Error Resume Next
            Minutes = VBA.Conversion.CLng(Temp(0))
            Seconds = VBA.Conversion.CDbl(Temp(1))
            On Error GoTo 0
            TotalSeconds = TotalSeconds + Minutes * 60 + Seconds
        End If
    End If
    
    ' ---------------------------------------------------------------------------------------------
    ' Convert third Time TextBox
    ' ---------------------------------------------------------------------------------------------
    sTime = Me.Time3.Text
    Minutes = 0
    Seconds = 0#
    If Not Me.Time3.Text = VBA.Constants.vbNullString Then
        Temp = VBA.Strings.Split(sTime, ":")
        If (UBound(Temp) - LBound(Temp) + 1) = 2 Then
            On Error Resume Next
            Minutes = VBA.Conversion.CLng(Temp(0))
            Seconds = VBA.Conversion.CDbl(Temp(1))
            On Error GoTo 0
            TotalSeconds = TotalSeconds + Minutes * 60 + Seconds
        End If
    End If
    
    ' ---------------------------------------------------------------------------------------------
    ' Convert fourth Time TextBox
    ' ---------------------------------------------------------------------------------------------
    sTime = Me.Time4.Text
    Minutes = 0
    Seconds = 0#
    If Not Me.Time4.Text = VBA.Constants.vbNullString Then
        Temp = VBA.Strings.Split(sTime, ":")
        If (UBound(Temp) - LBound(Temp) + 1) = 2 Then
            On Error Resume Next
            Minutes = VBA.Conversion.CLng(Temp(0))
            Seconds = VBA.Conversion.CDbl(Temp(1))
            On Error GoTo 0
            TotalSeconds = TotalSeconds + Minutes * 60 + Seconds
        End If
    End If
    
    ' ---------------------------------------------------------------------------------------------
    ' Convert it all into a displayable MM:SS.SS format
    ' ---------------------------------------------------------------------------------------------
    'MsgBox "Total Seconds = " & TotalSeconds
    Minutes = WorksheetFunction.RoundDown(TotalSeconds / 60, 0)
    Seconds = TotalSeconds Mod 60
    Point = TotalSeconds - ((Minutes * 60) + Seconds)
    Seconds = Seconds + Point
    Me.TotalTime.Text = VBA.Strings.Format(Minutes, "00") & ":" & VBA.Strings.Format(Seconds, "00.00")
End Sub
Please note that this is untested and any works should be saved before attempting to run it.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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