+ Reply to Thread
Results 1 to 2 of 2

Thread: Calculating Hours between Dates and Times VBA

  1. #1
    Registered User
    Join Date
    04-17-2011
    Location
    Orlando
    MS-Off Ver
    Excel 2007
    Posts
    27

    Exclamation Calculating Hours between Dates and Times VBA

    Problem,

    I am trying to create an application that records work time. So a user would record his start work time, and after the completion of the job, Record the End work time. I am having problems with the code that determines that hours needed for the job.

    So in the sample below, the code determines the last imput Start date and Start Time, and then Deduts that from the current Date and time.
    However I cant get the hours to come out right. If the project is too long, the code doesn't work right?

    help??


    Dim Formtimetwo As Date
    Time1 = Now()
    Timeround = Round(Time1 * 24 * 4) / 24 / 4
    Formtimetwo = Format(Timeround, "m/d/yyyy h:mm:ss AM/PM")
    'Current time Formated 
    
    'Below are what was previously recorded
    Pastdate = Ws2.Cells(Ws2.Rows.Count, 3).End(xlUp).Offset(0, 0).Value
    Pasttime = Ws2.Cells(Ws2.Rows.Count, 4).End(xlUp).Offset(0, 0).Value
    
    PastDateandTime = Pastdate + Pasttime
    Hours123 = Formtimetwo - PastDateandTime
    If Hours123 < "1" then
    TotalHours = Format(Hours123, "h:mm")
    Else
    TotalHours = Format(Hours123, "[h]:mm:ss")
    End if
    
    Range("Admin!E2").value = TotalHours

  2. #2
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,155

    Re: Calculating Hours between Dates and Times VBA

    Hi Crebsington
    Dont really follow what your doing but
    have a look at this example form the VBA help on DateDiff

    Dim TheDate As Date    ' Declare variables.
    Dim Msg
    TheDate = InputBox("Enter a date")
    Msg = "Days from today: " & DateDiff("d", Now, TheDate)
    MsgBox Msg
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0