Consulting

Results 1 to 2 of 2

Thread: Cut away days to have only hours : 45841.12 -> 0.12

  1. #1
    VBAX Regular
    Joined
    Mar 2016
    Posts
    12
    Location

    Cut away days to have only hours : 45841.12 -> 0.12

    Hi,

    I need to copy cells with vba. One cell has the format with days, the other cell has only hours. Obviously when i compare them, the cell with days will be bigger. Is there a simple trick to cut away the days ?

    I use this code:

    If tsm(s3).depart < (tsm(s3).fixedtime + WorksheetFunction.RoundDown(tsm(s3).depart, 0)) Then

    I managed to do this by adding the days, to the figure without. I would like to keep it more simple. If there is a possibility please let me know.

    Thank you

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Dates in Excel are number of whole and fractional days since Jan 1, 1900

    So number of days to the left of the decimal and parts of a day (i.e. time) to the right of the decimal


    Sub test()
        Dim d As Date
        
        d = Now
        
        MsgBox Format(d, "yyyy-mm-dd hh:mm:ss")
        
        MsgBox Format(d - Int(d), "hh:mm:ss")
     
    End Sub

    BUT there is a lot of precision (more decimals) that you might need to address. For example the Now above is really = 42452.6735185185. This means that the 'seconds' might look the same, but could be different in the 8th place. This might be another option

    Option Explicit
    Sub test()
        Dim WithDays As Date, WithoutDays As Date
        
        WithDays = Now
        WithoutDays = WithDays - Int(WithDays) + 0.000001
        
        If Format(WithDays, "hh:mm:ss") = Format(WithoutDays, "hh:mm:ss") Then
            MsgBox "Same"
        End If
    End Sub
    Last edited by Paul_Hossler; 03-23-2016 at 01:19 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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