Results 1 to 20 of 23

Thread: Work hours between 2 dates(not working -VBA)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    This code does it.

    Const WORKING_DAY_END As String = "18:00"
        Const FORMULA_WORKING_TIME As String = _
        "=(INT(E2-D2)*(""" & WORKING_DAY_END & """-""" & WORKING_DAY_START & """)" & _
        "+MEDIAN(MOD(E2,1),""" & WORKING_DAY_END & """,""" & WORKING_DAY_START & """)" & _
        "-MEDIAN(MOD(D2,1),""" & WORKING_DAY_END & """,""" & WORKING_DAY_START & """))"
        Dim lastrow As Long
        With ActiveSheet
             lastrow = .Cells(.Rows.Count, "G").End(xlUp).Row
             With Range("G2").Resize(lastrow - 1)
                  .Formula = FORMULA_WORKING_TIME
                  .Value = .Value
                  .NumberFormat = "[h]:mm"
            End With
        End With
    I have used all the constants so as to make it easy to amend if the start/end day times change. I have also output the answer as hh:mm as this makes more sense (to me) than decimals.
    Last edited by Aussiebear; 11-20-2024 at 12:24 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •