• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Date diff in userform textbox

Belleke

Well-Known Member
How to calculate de time difference between 2 textboxes in hours en minutes.
example
TextBox 1
10/10/2018 17:35
Textbox 2
11/10/2018 17:40
desired result in textbox 3
24:05
thanks in advance
 
Try this code
Code:
Private Sub CommandButton1_Click()
    Dim dt1 As String, dt2 As String, d As Double, mHours, mMinutes, mSeconds

    dt1 = TextBox1.Value
    dt2 = TextBox2.Value
    d = DateDiff("s", CDate(dt1), CDate(dt2))

    mHours = Format(d \ 3600, "00")
    mMinutes = Format((d - (mHours * 3600)) \ 60, "00")
    mSeconds = Format(d - ((mHours * 3600) + (mMinutes * 60)), "00")
    TextBox3.Value = mHours & ":" & mMinutes & ":" & mSeconds
End Sub
 
Personally I'd just use Int, Hour, Minute function.

Using YessarKhalil's code as base...
Code:
Private Sub CommandButton1_Click()
    Dim dt1 As String, dt2 As String, d As Double

    dt1 = TextBox1.Value
    dt2 = TextBox2.Value
    d = DateDiff("s", CDate(dt1), CDate(dt2)) / 60 / 60 / 24
    TextBox3.Value = Format(Int(d) * 24 + Hour(d), "00") & ":" & Format(Minute(d), "00")
End Sub
 
Back
Top