+ Reply to Thread
Results 1 to 15 of 15

Userform input start/stop time and duration

  1. #1
    Registered User
    Join Date
    09-27-2014
    Location
    Copenhagen
    MS-Off Ver
    2013
    Posts
    9

    Question Userform input start/stop time and duration

    Hi all

    Except YouTube tutorials I am complete new to VBA and this is my first thread ever, so be nice to me :-) . I have a course coming up in a month but I need to finish this ASAP, so I hope you guys will help me. I am making a time calculation project and I'm almost done, but I have a few hurdles that I can't pass:

    1) The time input is not showed correctly.

    - In Denmark we use 00:00-2359 and no AM or PM. To get the user to type only this kind of input I made some If-rules so the user can't type it differently. But I don't the user to type in the colon him self (e.g. from 1345 to 13:45). So basically I am asking how to do this a simple way?

    I have tried formatting the cells in Excel with no luck. I have also tried this code:

    Range("a1").Value = Left(Range("a1").Value, 2) & ":" & Right(Range("a1").Value, 2)

    The code works almost fine, but not if the time is before 10:00 (between 00:00 and 09:59). What can I do?

    2) Calculation across midnight
    - The other problem is when start time is before 23:59 and the stop time after 00:00.

    This is my code: Code.txt

    I have been trying for 5 hours and I haven't got any further. Can you help me please?


    Andy

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Userform input start/stop time and duration

    1:
    Please Login or Register  to view this content.
    2:
    Duration = (EndTime + 1*(StartTime > EndTime))-StartTime
    Last edited by protonLeah; 09-27-2014 at 06:34 PM.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    09-27-2014
    Location
    Copenhagen
    MS-Off Ver
    2013
    Posts
    9

    Re: Userform input start/stop time and duration

    Thank you so much - I am so grateful!

    No. 1 works perfectly but no. 2 does still not work across midnight.

    Do I need to do something special to make no. 2 work? Like a special formatting? Maybe I should mention that I have no Dim in my project.

    My code from you look like this now:

    Please Login or Register  to view this content.
    When I use e.g. 23:00 to 01:30 I get -1,89583333333333 as result of duration.

    Am I using your code no. 2 wrong?

    Thanks again!

    Andy

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Userform input start/stop time and duration

    Sorry, I forgot that VBA returns -1 for TRUE, not +1:
    Please Login or Register  to view this content.
    -1 * TRUE = +1
    Last edited by protonLeah; 09-28-2014 at 03:06 PM.

  5. #5
    Registered User
    Join Date
    09-27-2014
    Location
    Copenhagen
    MS-Off Ver
    2013
    Posts
    9

    Re: Userform input start/stop time and duration

    Flappy_Owl_V1.0.xlsm

    Amazing! Thanks! I have attached a VBA-game as a present.

    Let me know if I can return th favor one day (I'm good with network :-)).

    How do I mark the thread as SOLVED?

  6. #6
    Registered User
    Join Date
    09-27-2014
    Location
    Copenhagen
    MS-Off Ver
    2013
    Posts
    9

    Re: Userform input start/stop time and duration

    I'm sorry - I got too exited about your code. It is working but not for my purpose.

    Right night the code works like this:

    Start: 12:00
    End: 12:30
    Duration: 0,30

    What I need is this:

    Start: 12:00
    End: 12:30
    Duration: 0,50

    Can you help me again, protonLeah?

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: Userform input start/stop time and duration

    Try this:
    Please Login or Register  to view this content.
    Note: Cell must be formatted as number not time.

  8. #8
    Registered User
    Join Date
    09-27-2014
    Location
    Copenhagen
    MS-Off Ver
    2013
    Posts
    9

    Re: Userform input start/stop time and duration

    It's not working.

    Start: 17:00
    End: 17:30
    Duration: 16,79

    What can I do?

  9. #9
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Userform input start/stop time and duration

    Hello AndyAndersen,

    I cannot replicate your result which shows,"Duration: 16,79"

    Yet, a simple formula like: =(B1-A1)*24, returns your desired result.

    I would need to see the complete Code which returns the value incorrectly.

    Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  10. #10
    Registered User
    Join Date
    09-27-2014
    Location
    Copenhagen
    MS-Off Ver
    2013
    Posts
    9

    Re: Userform input start/stop time and duration

    A formula like: =(B1-A1)*24 will not work if start time is before midnight and end time is after midnight. That was why my problem started.

    Here is a part of the code:

    Please Login or Register  to view this content.
    I attach my full userform code too. Userform.txt

    You see?

  11. #11
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Userform input start/stop time and duration

    Hello AndyAndersen,

    Thank you for the feedback.

    Yes, you are correct about that formula, but it could be changed to: =IF(B1<A1,((B1-A1)*24)+24,(B1-A1)*24)

    As for your Code, please replace this line:

    Please Login or Register  to view this content.
    with

    Please Login or Register  to view this content.
    Hope that helps.

  12. #12
    Registered User
    Join Date
    09-27-2014
    Location
    Copenhagen
    MS-Off Ver
    2013
    Posts
    9

    Re: Userform input start/stop time and duration

    It's still not working - I'm so confused!

    Is your offset right?

    ActiveCell.Offset(0, 0) is the date
    ActiveCell.Offset(0, 1) is the start time
    ActiveCell.Offset(0, 2) is the end time
    ActiveCell.Offset(0, 3) is the duration

  13. #13
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Userform input start/stop time and duration

    You don't need "Offset(0, 0)", just use ActiveCell.

    Please try the Code now,

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    09-27-2014
    Location
    Copenhagen
    MS-Off Ver
    2013
    Posts
    9

    Re: Userform input start/stop time and duration

    Finally I think I'm there.

    Thank you all!!

    How do I mark the thread as solved?

  15. #15
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Userform input start/stop time and duration

    Please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Thanks.

+ 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. Calculate duration from start and end time entered
    By famico78 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-02-2014, 06:14 AM
  2. [SOLVED] Start Time and Stop Time: Disable End Button If Cell Is Blank
    By FLani in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-22-2013, 09:20 AM
  3. Calculate finish time given start time, working hours and job duration
    By swanseaexcel in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-23-2012, 03:00 PM
  4. Replies: 1
    Last Post: 02-23-2012, 09:01 AM
  5. Graph MSSQL Job Start Time And Duration
    By reckless in forum Excel General
    Replies: 2
    Last Post: 04-23-2009, 08:33 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