+ Reply to Thread
Results 1 to 16 of 16

Format textbox as a duration

  1. #1
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Format textbox as a duration

    Hi. I have a userform for data input. One of fields required is amount of time it took a worker to complete a job. How can I format the textbox so that for example if I typed in 0130 it would format it as 1hr 30mins.

    No task will take longer than 24 hours.

    Cheers for your help

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Format textbox as a duration

    Hi Chris,

    By definition textboxes are of a type variant. The user can input anything he/she wants. You can use the AfterUpdate event to format the data entered as shown below

    Please Login or Register  to view this content.
    quick and dirty, no validations
    If you like my contribution click the star icon!

  3. #3
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Format textbox as a duration

    Ollie as ever many thanks for your quick and correct response.

  4. #4
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Format textbox as a duration

    Ollie

    If I then had a text box with no. of people, what would I need to do to multiple number of people.value by duration. this is what I have but it fails:

    Please Login or Register  to view this content.

  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Format textbox as a duration

    The format instruction is just taking the value in the textbox and inserts a ':' in between for DISPLAY purposes only. It does not translate the value entered into hours and minutes so using the value as a source for your calculations of the field total hours is not going to work.

    you could try

    Please Login or Register  to view this content.
    Note that the above 'trick' does not work if the total number of hours exceeds 24

  6. #6
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Format textbox as a duration

    Hi Ollie. WIll test the above shortly. Sure it works though.

    Any chance there is a solution is the work exceeds 24 hours. sometimes we have 12 people on a job so whilst job may take only 4 hours it is easy to exceed total hours of 24.

  7. #7
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Format textbox as a duration

    HI Ollie

    Just tested it using the following
    Please Login or Register  to view this content.
    Getting nothing in total hours other than " : "

    Any thoughts?

  8. #8
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Format textbox as a duration

    Chris,

    created a form with the three textboxes, entered 3 in the people box and 0130 in the hours box. The value 0130 gets formatted as 01:30 and the total box shows 04:30. What values did you enter?

  9. #9
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Format textbox as a duration

    Hi Chris,

    I missed your post about > 24 hours. I have re-written the code. The input time may now be specified as 0130, 130 or 1:30 and the total can be higher than 24 hours

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Format textbox as a duration

    THis is working great but with the none validation issues with the hourstxt I can see errors occurring so was wondering whether a solution would be to have 2 text boxes. One for the hours and one for the minutes and somehow combine them in a third textbox in the format of a duration and apply the third text box to the above scenario. Thoughts as ever appreciated?

  11. #11
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Format textbox as a duration

    Hi Chris,

    not sure what you are saying. Are you still getting errors?

    I tried many combinations of input but am unable to replicate.

    Can you give me some examples?

  12. #12
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Format textbox as a duration

    apologies for not being clear.

    THe following code you provided works perfectly
    Please Login or Register  to view this content.
    WHat I am worried about now is the formatting code which allows you to enter duration in the hourstxt could lead to errors as has no validation. for example if I saw 1 hour on the timesheet and quickly typed in 1 in hourstxt I get 1 minute as opposed to 1hour and 0 minutes. SO I am wondering whether I replace the hourstxt with two text boxes labelled hours and minutes and then combine there values to form a duration in a third textbox.

    Hope that makes more sense.

  13. #13
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Format textbox as a duration

    it does :-)

    having two different input boxes for hours and minutes certainly allows for more reliable data entry and validation. Adjusting the code to deal with this is quite easy:

    Use the AfterUpdate event for the separate textboxes to check whether the value entered is correct (actually only on the minutes textbox which cannot exceed 59), and make the following adjustments to the code

    replace
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    and, replace

    Please Login or Register  to view this content.
    with

    Please Login or Register  to view this content.
    and, finally remove

    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Format textbox as a duration

    Sorted that bit thankyou. Sorry I knew how to do that I was just being stupid.

    Ok final problem I promise (at least I think it is the final problem) - I have 2 systems I have to enter these hour figures into. one works in hours and minutes and the other works in decimal time. SO how to I turn 2hours and 40 minutes into 2.66 for a final text box

  15. #15
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Format textbox as a duration

    ahhh we crossed over in cyber space. MAny thanks for your suggestion - as ever it is far more elegant than mine.

  16. #16
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Format textbox as a duration

    Quote Originally Posted by chrisjames25 View Post
    Sorted that bit thankyou. Sorry I knew how to do that I was just being stupid.

    Ok final problem I promise (at least I think it is the final problem) - I have 2 systems I have to enter these hour figures into. one works in hours and minutes and the other works in decimal time. SO how to I turn 2hours and 40 minutes into 2.66 for a final text box
    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Format textbox as a duration

    As ever annoying I can't add more rep for your assistance. Promise will do when add to someone else

+ 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. [SOLVED] Formula: To calculate duration between two times, if duration is over 6hours subtract 30mi
    By MattBarnes7 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-11-2013, 03:22 PM
  2. Custom format to display numbers as duration
    By pacody in forum Excel General
    Replies: 2
    Last Post: 11-30-2009, 06:33 PM
  3. Help in converting duration to hrs format
    By gloom52 in forum Excel General
    Replies: 3
    Last Post: 09-02-2009, 06:30 PM
  4. Format cell to show duration of phone calls
    By Dana in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-10-2006, 11:15 AM
  5. Format cells for Duration?
    By G-Man in forum Excel General
    Replies: 6
    Last Post: 02-09-2005, 06:06 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