1. Welcome to Chandoo.org Forums. Short message for you

    Hi Guest,

    Thanks for joining Chandoo.org forums. We are here to make you awesome in Excel. Before you post your first question, please read this short introduction guide. When posting or responding to questions please remember our values at Chandoo.org are: Humility, Passion, Fun, Awesomeness, Simplicity, Sharing Remember that we have people here for whom English is not there first language and we need to allow for this in our dealings.

    Yours,
    Chandoo
  2. 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...

  3. 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

Discussion in 'VBA Macros' started by Belleke, Oct 10, 2018.

  1. Belleke

    Belleke Active Member

    Messages:
    458
    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
  2. YasserKhalil

    YasserKhalil Active Member

    Messages:
    997
    Try this code
    Code (vb):
    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
    Belleke likes this.
  3. Chihiro

    Chihiro Excel Ninja

    Messages:
    5,006
    Personally I'd just use Int, Hour, Minute function.

    Using YessarKhalil's code as base...
    Code (vb):
    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
    Belleke and YasserKhalil like this.
  4. Belleke

    Belleke Active Member

    Messages:
    458
    Hi YasserKhalil and Chihiro,
    Thank you for helping me out.
    Both solutions work, so my problem is solved.
    Have a nice day.
    YasserKhalil likes this.
  5. YasserKhalil

    YasserKhalil Active Member

    Messages:
    997
    You're welcome. Glad we can offer some help
    Regards

Share This Page