Results 1 to 2 of 2

Thread: manipulating time in VBA

  1. #1
    Board Regular
    Join Date
    Apr 2002

    Default manipulating time in VBA

    Hi I need to add time to a variable in VBA. I have tried every possible variable because what I need to do is to first convert that value into some form where I can add or delete time ie in minutes or hours and then I need to convert it back to time value.

    I do this by using VBA.TimeValue(String/Date)

    But from here I dont know how to manipulate ie add or deleite time from the above if I assign it to some variable

    How can I do this? in VBA

    Thanks in advance.

  2. #2
    MrExcel MVP
    Andrew Poulsom's Avatar
    Join Date
    Jul 2002

    Default Re: manipulating time in VBA

    You just need to dimension your variable as Date, which is stored as IEEE 64-bit (8-byte) floating-point numbers that represent dates ranging from 1 January 100 to 31 December 9999 and times from 0:00:00 to 23:59:59. Values to the left of the decimal represent date information while values to the right of the decimal represent time. Midnight is 0 and midday is 0.5. Negative whole numbers represent dates before 30 December 1899.

    Then you can perform simple arithmentic, eg:

    Sub Test()
        Dim MyDate As Date
        MyDate = TimeValue("08:35:17")
        MsgBox WorksheetFunction.Text(MyDate, "[hh]:mm:ss")
        MyDate = MyDate + TimeValue("12:12:12")
        MsgBox WorksheetFunction.Text(MyDate, "[hh]:mm:ss")
        MyDate = MyDate + 0.5
        MsgBox WorksheetFunction.Text(MyDate, "[hh]:mm:ss")
    End Sub

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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