Results 1 to 2 of 2
  1. #1
    3 Star Lounger
    Join Date
    Aug 2004
    Posts
    361
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Military time (Excel VBA)

    The following code is suppose to do as it says in the first line. The code was found http://www.cpearson.com/excel/DateTimeVBA.htm. I copied it and pasted into Excel VBA editor. When tried it and it doesn't appear to work. Is there some error in the code, that may have not been entered properly on the site

    ' Military To Time ' This function converts military time, supplied as an integer,to a standard serial time number. For example, it converts 1130 to 11:30, and 1650 to 16:50.Public Function MilitaryToTime(T1 As Integer)
    '
    ' Input T1: 24-hour time as integer,e.g., 1130=11:30, 1650=16:50
    ' Output, time as serial time e.g, 0.5 for noon.
    '
    Dim TT1 As Double TT1 = Int(T1 / 100) + (((T1 / 100) - Int(T1 / 100)) / 0.6) '23.50
    TT1 = TT1 / 24
    MilitaryToTime = TT1 End Function

    Thanks for any help. Also, I am really new to VBA and would like to know if anyone can recommend a beginner's level book.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Military time (Excel VBA)

    Some line ends have disappeared from the code, making it invalid. Here is the corrected code.
    - Activate the Visual Basic Editor (Alt+F11).
    - If you haven't created a module yet, select Insert | Module.
    - Copy the following code into the module:

    Public Function MilitaryToTime(T1 As Integer)
    '
    ' Input T1: 24-hour time as integer,e.g., 1130=11:30, 1650=16:50
    ' Output, time as serial time e.g, 0.5 for noon.
    '
    Dim TT1 As Double
    TT1 = Int(T1 / 100) + (((T1 / 100) - Int(T1 / 100)) / 0.6) '23.50
    TT1 = TT1 / 24
    MilitaryToTime = TT1
    End Function

    - Switch back to Excel.
    - Enter a number such as 1130 or 1650 in a cell, say in A1.
    - Enter the following formula in another cell, say in B1:

    =MilitaryToTime(A1)

    - Cell B1 will probably display a number, not a time.
    - Select cell B1 again if necessary.
    - Select Format | Cells...
    - Select the Time category.
    - Select your favorite time format.
    - Click OK.

Posting Permissions

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