+ Reply to Thread
Results 1 to 12 of 12

Help counting hours in VBA

  1. #1
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Question Help counting hours in VBA

    Hi all,
    I've found some code which i'm using for a staff scheduling system. It can turn strings such as 12pm-5pm into time and then work out the hours which is great, it will also add them up for any given working week. However, what I really need is some more code that will count down the rows rather than the columns and give me a total employee count per hour.
    For instance, I have 3 employees - A, B, and C.
    Name Monday
    A 11am - 3pm
    B 12pm - 6pm
    C 12pm - 9pm
    What I'd like to be able to do for any given day is have a count for how many hours are being spent, per hour. So using the above table it would count 1 hour for 11am-12pm, 3 hours from 12pm -1pm, etc.

    Please see the current code below, if you can see a way I can extrapolate the data I need from the data already created then I'd be really grateful, my VBA skills are very limited as I only delve into it when standard formulas won't cut it. I appreciate any help that you guys and gals can give.

    Please Login or Register  to view this content.
    Thank you in advance!
    ~Wabbit

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Help counting hours in VBA

    Hello WarWabbit,

    Welcome to the Forum!

    Do the times start in column "G"?

    What is the starting row?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    Hi Leith,
    Yes, the times start in the G column - so run G-M (Mon-Sun).
    The starting row is 6.

    Thanks,
    ~Wabbit

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Help counting hours in VBA

    Hello WarWabbit,

    The macro will replace the times in columns G - M with the hours, correct?

    So, 11am - 3pm would become 5 or would it be 5 hours?

  5. #5
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    Hi,
    It won't actually change the cell value, it will leave them as a string so the user can see the shift that has been input, the vba code captures the daily hours which I can then output as a day or as the whole week, which is what I need. It's the ability to strip the hours of a day into the appropriate times. So with a 12pm - 3pm shift I need to be able to output that 1 hour has been worked at 12pm, 1pm and 2pm. If it's easier I can link you the file if there is a way to do that?

    Cheers,
    ~Wabbit
    Last edited by WarWabbit; 12-29-2016 at 01:47 PM.

  6. #6
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    Posted twice, please delete.
    Last edited by WarWabbit; 12-29-2016 at 01:05 PM.

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Help counting hours in VBA

    You can post a copy of the workbook here at the forum.

    To Attach a File:

    1. Scroll down to the window below your post Additional Options
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit.

  8. #8
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    Thanks,
    Please ignore everything from the O column onwards, it's not applicable to the general workflow and is the next stage of reporting i'm working on.

    Cheers,
    ~Wabbit
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    I'm not sure of the scope of Vars in VBA, is it possible to create a countif function capturing the 'Hours' as it goes through the GetLineHours function?

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Help counting hours in VBA

    Hello WarWabbit,

    I rewrote the UDF. It is much shorter and recalculates whenever a change is made to a dependent cell. All of the =Hours() UDF calls have been replaced with the new UDF call =CalculateHours()

    Module2 UDF Macro Code

    Please Login or Register  to view this content.
    Attached Files Attached Files

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Help counting hours in VBA

    Hello WarWabbit,

    I cleaned the code up a little. This removes some unnecessary redundancy.

    Improved UDF Code
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    12-29-2016
    Location
    England
    MS-Off Ver
    2013
    Posts
    16

    Re: Help counting hours in VBA

    Thank you for improving the code. Did you have any luck with it being able to split an employee's hours into individual hours?

    Cheers,
    ~Wabbit

+ 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. Elapsed working hours, without counting weekends or non-working hours
    By ebkiwi in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-16-2013, 04:18 PM
  2. Counting difference between hours if outside of business hours
    By joytech22 in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 07-15-2013, 05:03 AM
  3. Excel 2007 : counting hours
    By stonefacedolly in forum Excel General
    Replies: 10
    Last Post: 03-17-2010, 05:27 PM
  4. Counting Hours
    By Don Juan in forum Excel General
    Replies: 4
    Last Post: 11-17-2009, 01:16 PM
  5. Counting hours on a timesheet
    By tazmanian24 in forum Excel General
    Replies: 2
    Last Post: 10-15-2009, 10:05 AM
  6. Counting hours
    By Analyst99 in forum Excel General
    Replies: 3
    Last Post: 10-26-2006, 07:20 PM
  7. Counting hours
    By vanilla_bean_orange via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-17-2006, 07:15 AM

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