+ Reply to Thread
Results 1 to 2 of 2

Time Formatting

Hybrid View

  1. #1
    Registered User
    Join Date
    02-18-2008
    Posts
    1

    Time Formatting

    I would like to avoid using the colon when entering a time. Can a cell be formatted to read the a military time like 1525 (not 15:25) and convert it to 3:25pm?

    Thanks in advance

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello rtcol,

    Excel doesn't provide a time format without a colon. If you wanted to enter the numbers in this way, you would need a macro. The macro would then examine the cells, make the conversion, and then set the cell's value to the am/pm format.

    Here is an example that will convert any valid 24 hour entry (less the colon) in the range of D2 to D10 of the Worksheet you choose (see the installation instructions).
    Private Sub Worksheet_Change(ByVal Target As Range)
      
      Dim Hrs As Integer
      Dim Minutes As Integer
      Dim TimeRng As Range
      
      Application.EnableEvents = False
      
        Set TimeRng = ActiveSheet.Range("D2:D10")
          If Not Intersect(Target, TimeRng) Is Nothing Then
             If IsEmpty(Target) Then GoTo LeaveSub
             If Target.Value < 0 Or Target.Value > 2400 Then GoTo LeaveSub
                Hrs = Target \ 100
                Mins = Target - (Hrs * 100)
             Target = Format(TimeSerial(Hrs, Mins, 0), "hh:mm am/pm")
          End If
          
    LeaveSub:
      Application.EnableEvents = True
      
    End Sub
    How to Save a Worksheet Event Macro
    1. Copy the macro using CTRL+C keys.
    2. Open your Workbook and Right Click on the Worksheet's Name Tab for the Worksheet the macro will run on.
    3. Left Click on View Code in the pop up menu.
    4. Paste the macro code using CTRL+V
    5. Save the macro in your Workbook using CTRL+S

    Sincerely,
    Leith Ross

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1