Consulting

Results 1 to 7 of 7

Thread: Calculating Endtime/Duration

  1. #1
    VBAX Newbie
    Joined
    Jan 2013
    Posts
    4
    Location

    Calculating Endtime/Duration

    Hi I'm creating a time-sheet in Excel and want to be able to calculate the duration based on start and end time, however I also want it to be able to calculate the end time if end time cell is left blank and duration is filled in? I'm quite new to VBA so i hope you can help! Thanks

  2. #2
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    Hi lukayeh, can you post a sample workbook to show how your data will be placed on the worksheet? Otherwise you can get code designed for columns when you're working in rows etc.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  3. #3
    VBAX Newbie
    Joined
    Jan 2013
    Posts
    4
    Location
    Well i haven't quite figured out the layout yet but lets say:
    A2 the start time formatted hh:mm AM/PM
    B2 the end time formatted hh:mm AM/PM
    C2 the duration hh:mm "hrs"
    What i want for is if B2 is left empty and C2 is filled in B2 is calculated but if B2 is filled in, C2 will be calculated, just not sure how to go about doing it as at somepoint all three could be filled in causing confusion

  4. #4
    VBAX Newbie
    Joined
    Jan 2013
    Posts
    4
    Location
    I've attached my spreadsheet, what i've managed to do so far, i've added spinners to the start and end times but these might over complicate things as they require formulas in the start and end cells
    Attached Files Attached Files

  5. #5
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    The attached spreadsheet has some code that might help you. It's pretty raw though, you'd probably need error handling.
    Attached Files Attached Files
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

  6. #6
    VBAX Newbie
    Joined
    Jan 2013
    Posts
    4
    Location
    That works great, I'm looking to use a dialog box to change the times, however when submitted this doesn't update the values, for example if i use the dialog to change start and finish times it doesn't update the duration, I understand that the macro runs when the worksheet is changed but how do i do it so it updates with dialog input?
    Thanks for your help so far!

    [VBA]Sub Transfer_Time()

    Worksheets("Sheet1").Range("a2").Value = DialogSheets("Dialog1").EditBoxes("Edit Box 4").Text
    'Worksheets("Sheet1").Range("b2").Value = DialogSheets("Dialog1").EditBoxes("Edit Box 6").Text
    Worksheets("Sheet1").Range("c2").Value = DialogSheets("Dialog1").EditBoxes("Edit Box 8").Text

    End Sub[/VBA]

    As i said i'm new to VBA so that code is very basic for the dialog box

  7. #7
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    I haven't used DialogSheets myself but I'd recommend using a UserForm over a DialogSheet. DialogSheets were replaced by UserForms quite a while ago (excel 95?) and have really been maintained for backward compatibility. In excel 2007 onwards they're hidden objects to the object browser so you won't get any IntelliSense help when trying to write code for them.
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •