+ Reply to Thread
Results 1 to 7 of 7

Calculating Hours worked per month in VBA Excel

  1. #1
    Registered User
    Join Date
    08-08-2022
    Location
    CT, USA
    MS-Off Ver
    Microsoft 365
    Posts
    11

    Calculating Hours worked per month in VBA Excel

    I am attempting to help out a coworker with at least an idea of how to approach this problem.

    Shown below is the data that was given to me. It contains the start and end dates of a job, hours worked, and if the job was over 1 month of work. I am trying to pull the hours worked per month for further calculations not related to this question. However, issues arise when jobs last over a month as I need to appropriately account for the hours in the correct month. For example the job in A2 and B2: it began in January and finished in May. How could I take the hours worked and evenly spread them across Jan, Feb, Mar, Apr, and May.

    excelHoursWorked.png

    I know to check the "Over 1 Month?" column for "YES" and also how to divide the hours for the correct number of months worked but I am stuck on how to get VBA to decipher which months to distribute the hours across with only MM/DD/YYYY start and end dates.

    I am looking for any help at all, even just a rough idea on how to decipher which months to spread the hours across. Just trying to figure out how to approach the problem, thank you.

  2. #2
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Calculating Hours worked per month in VBA Excel

    I would find the earliest date in the labor begins column and the most recent date in the work ends column and create a new column for each month between that time.

    Then I would go line by line in your date and average the time for each row... for example, your first column is Jan - May 2019, 168.5/5 = 33.7 a month... if there are overlaps, simply add to the running total.

    I could write you a code pretty quickly if you want what I just described.

  3. #3
    Registered User
    Join Date
    08-08-2022
    Location
    CT, USA
    MS-Off Ver
    Microsoft 365
    Posts
    11

    Re: Calculating Hours worked per month in VBA Excel

    Okay that does make sense. I appreciate the example as I get the idea of the process required too.

    I would greatly appreciate a code snippet if possible but no problem if not.

    Thank you.

  4. #4
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: Calculating Hours worked per month in VBA Excel

    hello
    like this
    =MAX(0;MIN(G$1;$B3+1)-MAX(F$1;$A3))/$D3*$C3
    Attached Files Attached Files
    Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

  5. #5
    Registered User
    Join Date
    08-08-2022
    Location
    CT, USA
    MS-Off Ver
    Microsoft 365
    Posts
    11

    Re: Calculating Hours worked per month in VBA Excel

    Thank you, that really helped to point me in the right direction. Much appreciated.

  6. #6
    Registered User
    Join Date
    08-08-2022
    Location
    CT, USA
    MS-Off Ver
    Microsoft 365
    Posts
    11

    Re: Calculating Hours worked per month in VBA Excel

    Quote Originally Posted by bsalv View Post
    hello
    like this
    =MAX(0;MIN(G$1;$B3+1)-MAX(F$1;$A3))/$D3*$C3
    I was just wondering if you could explain how this formula works? I've seen that it is flawless but I would like to learn how it actually works to garner a better understanding.

  7. #7
    Forum Expert
    Join Date
    01-25-2011
    Location
    Belgium, Alveringem
    MS-Off Ver
    Excel 2003, 2007, 365
    Posts
    1,418

    Re: Calculating Hours worked per month in VBA Excel

    MIN(G$1;$B3+1) = take smallest value of "the 1st day of next month" & "the day labors ends +1" (add one because you also work that last day)
    MAX(F$1;$A3) = take the greatest value of "the 1st day of this month" & "the day labor starts"
    then you substract the 2nd from the first and that result may never be negative, that's why you have max(0, min( )-max( ))
    This means the days you worked this month
    If you now multiply it with the hours worked (C3) and divide it by the total worked days (D3) (=enddate +1 - begindate) you get the hours assigned to this month.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Excel 2016- calculating hours worked- Military time
    By JusticeLC in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-07-2022, 03:51 PM
  2. [SOLVED] Calculating multiple pay rates based on time of day worked, not number of hours worked
    By vdbonce in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 03-04-2020, 02:30 AM
  3. Replies: 8
    Last Post: 09-30-2017, 07:00 PM
  4. Replies: 0
    Last Post: 05-14-2012, 05:36 PM
  5. Calculating basic hours worked, between a time range and premium hours worked
    By RoyLittle0 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-07-2012, 06:59 AM
  6. Replies: 0
    Last Post: 01-05-2012, 06:23 AM
  7. Calculating hours worked in excel
    By Crispyduck in forum Excel - New Users/Basics
    Replies: 0
    Last Post: 03-30-2005, 12:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1