+ Reply to Thread
Results 1 to 8 of 8

VBA I want to calculate the difference between two days excluding the weekends

  1. #1
    Registered User
    Join Date
    10-05-2019
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    8

    Unhappy VBA I want to calculate the difference between two days excluding the weekends

    Hi,

    I have this problem for 2 days now, I keep on trying to calculate the two dates that will give me the total fines, The Total fines can be calculated by the number of days that the book has been burrowed excluding weekends(Saturday and Sunday) which means "Date Today - Date Due" but I need to calculate the fines without counting the weekends. For example The Due Date was on Oct 04, 2019 and The book was returned on Oct 07, 2019 the total fines should only be "1" not "3".

    I just dont know what code to use.

    Please see Image, Code Thanks. Below Thanks.

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Capture.PNG

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: VBA I want to calculate the difference between two days excluding the weekends

    Could you not just use:

    Please Login or Register  to view this content.
    ?

    WBD
    Office 365 on Windows 11, looking for rep!

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: VBA I want to calculate the difference between two days excluding the weekends

    You can use Application.NetworkDays_Intl (I prefer to use Application.FunctionName rather than Application.WorksheetFunction.FunctionName, since Application.FunctionName will return ErrorValue rather than throwing application error when value not found etc).

    Ex:
    Please Login or Register  to view this content.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  4. #4
    Registered User
    Join Date
    10-05-2019
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    8

    Re: VBA I want to calculate the difference between two days excluding the weekends

    Hi WideBoyDixon,
    Wow, It worked. Thanks a lot. I owe you one.

  5. #5
    Registered User
    Join Date
    10-05-2019
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    8

    Re: VBA I want to calculate the difference between two days excluding the weekends

    Hi, I am having a problem with the code right now.
    The value in FinesTxtbox.Value Keep on returning 21, how do i solve this?
    Attachment 644536

    Please Login or Register  to view this content.

  6. #6
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: VBA I want to calculate the difference between two days excluding the weekends

    There is no attachment. However, I'd also need to know what values you were putting into the two date fields and why 21 is incorrect.

    WBD

  7. #7
    Registered User
    Join Date
    10-05-2019
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    8

    Re: VBA I want to calculate the difference between two days excluding the weekends

    Quote Originally Posted by WideBoyDixon View Post
    There is no attachment. However, I'd also need to know what values you were putting into the two date fields and why 21 is incorrect.

    WBD
    Okay in my office computer the sum of the value is 21 the dates that I am subtracting are 10/08/2019 and 10/09/2019 the value is always 21 but in my laptop the value is correct. Is there something wrong with my office computer or is it just that my laptop is better.

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,882

    Re: VBA I want to calculate the difference between two days excluding the weekends

    I assume that you are using value/text stored in cell to supply these dates to the code.

    What is likely happening is that you are supplying dates in "dd/mm/yyyy" format, but in VBA dates are always treated using US format (mm/dd/yyyy).

    When passing date/time value from cell to code. You should always use .Value2. This ensures that underlying integer/decimal value is passed to code and not formatted date.

    Ex:
    Please Login or Register  to view this content.

+ 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. Replies: 5
    Last Post: 08-24-2015, 09:28 PM
  2. Calculate number of days excluding holidays and weekends
    By david1987 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 12-05-2012, 08:17 AM
  3. Replies: 3
    Last Post: 06-05-2011, 12:10 PM
  4. Replies: 7
    Last Post: 01-11-2011, 06:26 AM
  5. How to calculate elapsed time between to days excluding weekends.
    By Mush001 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-23-2009, 04:19 PM
  6. Calculate difference between dates excluding weekends
    By nitinjoshi123 in forum Excel General
    Replies: 15
    Last Post: 05-27-2008, 06:32 PM
  7. Replies: 3
    Last Post: 03-31-2008, 01:27 PM

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