Consulting

Results 1 to 9 of 9

Thread: Very omplicated calculation of hours worked (PLEASE HELP)

  1. #1
    VBAX Newbie
    Joined
    May 2016
    Posts
    4
    Location

    Very omplicated calculation of hours worked (PLEASE HELP)

    Hi. First of all, I have cross-posted the question below because it pertains to my job. I am unable to share the links to the other forums because my account is new.

    Anyway, here is the question:

    I have a spreadsheet that shows the amount of time that somebody worked over the course of several years. The spreadsheet is about 30,000 rows long and very poorly organized (more on this below). I need to calculate the hours worked by this individual each day over the course of all the years shown on the spreadsheet, but there are NOT separate columns for time clocked in and time clocked out. If there were separate columns for those values, I could calculate the total hours worked on the first day/row, and then I could drag the formula down the entire spreadsheet.

    Unfortunately, as I said, there are not separate columns for the clock-in and clock-out times. Instead there is one column for both times. To make matters worse, the column that shows times does not only show time clocked in and time clocked out; it also shows the times at which various work tasks were performed throughout the day (between the clock-in and clock-out times).

    Anyway, my boss wants me to figure out the total number of work hours recorded on the spreadsheet, and I have no idea how to proceed. The column that shows times (clocked in, clocked out, etc.) is right next to a column that shows the date (which is entered as a number rather than a date). Also, the date is written in the format YYYYMMDD, just to make things more confusing. So this is what the spreadsheet looks like:

    DATE | TIME | ACTIVITY
    20080507 | 9:30 | work task #1 (i.e. clock-in time)
    20080507 | 11:30 | work task #2
    20080507 | 2:30 | work task #3
    20080507 | 4:30 | final work task (i.e. clock-out time)
    20080508 | 9:30 | work task #1 (i.e. clock-in time)
    20080508 | 11:30 | work task #2
    20080508 | 2:30 | work task #3
    20080508 | 4:30 | final work task (i.e. clock-out time)
    20080509 | 9:30 | work task #1 (i.e. clock-in time)
    20080509 | 11:30 | work task #2
    20080509 | 2:30 | work task #3
    20080509 | 4:30 | final work task (i.e. clock-out time)

    (NOTE: The actual spreadsheet contains extreme variation between the days in terms of the hours worked on each day. Also, the terms "clock-in" and "clock-out" are NOT actually present anywhere in the spreadsheet. Thus, the stuff that appears in the Activity column is totally inconsistent and not very helpful in constructing a formula.)

    Anyway, given the strange format of this spreadsheet, I have no idea how to automatically calculate the total hours worked down the entire spreadsheet. The only thing that I can do is create formulas for each day (one day at a time) and then add up all the hours once I've calculated them day-by-day. The process would take months, which is too long for the demands of my boss. By the way, I have to do this for 7 other spreadsheets that are also about 30,000 rows long.

    Is there any way to create formulas that can 1) divide the rows into segments based on the DATE values 2) automatically calculate the total time elapsed within each segment/day (using the first and last time values for each segment/day, which would represent, respectively, time clocked-in and time clocked-out)?

    As you can see, this problem far exceeds my Excel capabilities. I truly appreciate any and all help that you guys can offer. Thank you.

  2. #2
    snb
    Guest
    You can alway post the address(es) where you crossposted (without it being a direct link). That's not too 'omplicated'.
    Nor is it to post a sample file.

  3. #3
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    It will be a long looping process. Best to put the date time columns into a array for speed.

    Basically:
    arrDatetimes = Range(Range("A1"), Range("B1").End(xlDown))
    i = Lbound(arrDateTimes)
    
    Do while i < Ubound(arrDateTimes)
       StartTime = arrDateTimes(i, 2)
    
       'Find last instance this date
       Do While arr(Datetimes(i, 1) = arrDateTimes(i + 1, 1) 
         i = i + 1
       Loop
    
       hoursWorked = HoursWorked + (ArrDatetimes(i, 2) - Starttime)
      i = i + 1
    Loop
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,837
    Location
    Assumes that Date and Time and Activity are in different columns

    BTW, the End times are in AM (i.e. < .5 day) so that needs to be handled


    Option Explicit
    Sub MarkStartStop()
        Dim i As Long, iStart As Long, iEnd As Long
        Dim rData As Range
        
        Application.ScreenUpdating = False
        
        With ActiveSheet
            .Cells(1, 1).End(xlToRight).Offset(0, 1).Value = "StartOrStop"
            .Cells(1, 1).End(xlToRight).Offset(0, 1).Value = "Duration"
            Set rData = .Cells(1, 1).CurrentRegion
        End With
        
        With rData
            
            .Cells(2, 4).Value = "Start"
            .Cells(2, 4).Interior.Color = vbGreen
            
            For i = 3 To .Rows.Count - 1
                Application.StatusBar = i
                If .Cells(i, 1).Value < .Cells(i + 1, 1).Value Then
                    .Cells(i, 4).Value = "Stop"
                    .Cells(i, 4).Interior.Color = vbRed
                
                ElseIf .Cells(i, 1).Value > .Cells(i - 1, 1).Value Then
                    .Cells(i, 4).Value = "Start"
                    .Cells(i, 4).Interior.Color = vbGreen
                
                Else
                    .Cells(i, 4).Value = "Working"
                    .Cells(i, 4).Interior.Color = vbYellow
                End If
            Next i
        
            .Cells(.Rows.Count, 4).Value = "Stop"
            .Cells(.Rows.Count, 4).Interior.Color = vbGreen
            
            iStart = 2
            iEnd = 2
            
            Do While iEnd < .Rows.Count
                Do
                    iEnd = iEnd + 1
                Loop Until .Cells(iEnd, 4).Value = "Stop"
                
                If .Cells(iStart, 2) > .Cells(iEnd, 2).Value Then
                    'end times all seem to be in AM
                    .Cells(iStart, 5).Value = 24 * (.Cells(iEnd, 2).Value + 0.5 - .Cells(iStart, 2).Value)
                Else
                    .Cells(iStart, 5).Value = 24 * (.Cells(iEnd, 2).Value - .Cells(iStart, 2).Value)
                End If
        
                iStart = iEnd + 1
            Loop
        
        End With
        Application.ScreenUpdating = True
        Application.StatusBar = False
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Newbie
    Joined
    May 2016
    Posts
    4
    Location
    Paul and Sam,

    Thank you so much. I'll follow up if I have any difficulties.

  6. #6
    VBAX Newbie
    Joined
    May 2016
    Posts
    4
    Location
    Fabulous stuff. A thousand thanks. :-)

    As I've said in the other forums, I took up too many people's time with this by posting in too many different places. For that, I apologize.

  7. #7
    snb
    Guest
    I thought MS invented pivottables for this purpose.
    Attached Files Attached Files

  8. #8
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    Are you ready to use the Tread Tools at the top to mark this solved?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  9. #9
    VBAX Newbie
    Joined
    May 2016
    Posts
    4
    Location
    Yep, I'll mark it as solved. Thanks.

Posting Permissions

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