+ Reply to Thread
Results 1 to 7 of 7

Converting a time difference calculation worksheet formula to a VBA Function

  1. #1
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Converting a time difference calculation worksheet formula to a VBA Function

    Hi everyone,

    I have the following worksheet formula to calculate the difference between two times with observance that end time is always greater than start time:

    Please Login or Register  to view this content.
    For Example: The difference between 08:00 PM And 02:00 AM is 6 hours

    Actually the function works just fine on the worksheet, but when I converted it to VBA using the following code:

    Please Login or Register  to view this content.
    The function gives correct result only when the start time is less than end time.

    Any suggestions?

    Regards,
    Last edited by LoveCandle; 03-19-2012 at 03:26 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Converting a time difference calculation worksheet formula to a VBA Function

    In Excel, a logical expression coerced to a number is 1 if True and 0 if False.

    In VBA, True coerced to a Long is -1, not 1.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: Converting a time difference calculation worksheet formula to a VBA Function

    Do you mean that it is impossible to implement the same formula in VBA?

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Converting a time difference calculation worksheet formula to a VBA Function

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: Converting a time difference calculation worksheet formula to a VBA Function

    Sorry, It didn't work,

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Converting a time difference calculation worksheet formula to a VBA Function

    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    11-07-2005
    Posts
    280

    Re: Converting a time difference calculation worksheet formula to a VBA Function

    Thank you very much shg for the effort you exerted to help me, but unfortunately your last code doesn't give correct result.

    Anyhow, I solved the problem by linking the combo boxes with worksheet cells and do the time difference calculation on the worksheet and transfer the result to the user form.

    Here are my codes:

    Cell Y6 Formula:
    Please Login or Register  to view this content.
    ComboBox3 Code:
    Please Login or Register  to view this content.
    ComboBox4Code:
    Please Login or Register  to view this content.
    MyTimes Function:
    Please Login or Register  to view this content.
    Best Regards,

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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