+ Reply to Thread
Results 1 to 3 of 3

VBA Check Time excluding hours

  1. #1
    Registered User
    Join Date
    11-06-2011
    Location
    Surrey
    MS-Off Ver
    Excel 2010
    Posts
    51

    VBA Check Time excluding hours

    Guys

    is there a way of adding time to a date/time with exclusions?

    I need to add a number of hours to a date/time which I do using DateAdd

    However if that time is between 5pm and 730 am the next morning I need the date returned to be the following day and time to be plus the reminder

    For instance if the date time is cell A1 is "10/04/2015 16:00:00" then this plus three hours would be "10/04/2015 19:00:00" The code I use is

    d = DateAdd("h", 3, Worksheets("Sheet1").Range("A1").Value)

    The date I need it to come back with is "11/04/2015 09:30:00" as anything between 1700 and 0730 should be disallowed.

    Any ideas?

    Mark

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,662

    Re: VBA Check Time excluding hours

    What are the assumptions about what the start time will be? Suppose the start time is 05:30? Is the correct result 08:30? Or 10:30?

    Here is a solution for the case you describe, generalized for adding any number of hours. I used arithmetic on the time instead of DateAdd.

    The start time is assumed to be after 07:30 and before 17:00. If your cases are more complex, you need to provide more detail.

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    11-06-2011
    Location
    Surrey
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: VBA Check Time excluding hours

    Thank you so much

    can I modify it for different Start times as I forgot about weekends

    as it runs it stops at 5pm and re starts at 730am which is fine

    If the date supplied is on a Friday then the Stop time is still 5pm but the restart time is the following Monday at 730am

    I suppose I could just add 2 days to the result unless there is a better way.

    Mark

+ 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. Excluding weekend hours from time
    By flapface in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-17-2014, 12:49 PM
  2. Need help Calculating Elasped time excluding non business hours.
    By hellpme in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-29-2013, 03:52 PM
  3. Time Tracking, excluding nonworking hours.
    By hayduck in forum Excel General
    Replies: 8
    Last Post: 07-05-2012, 11:57 AM
  4. Time between timestamps excluding certain hours
    By Silaswulff in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-01-2011, 06:08 PM
  5. Replies: 0
    Last Post: 04-07-2011, 01:46 AM

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