+ Reply to Thread
Results 1 to 16 of 16

Enter Date and Time quick way

Hybrid View

  1. #1
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Enter Date and Time quick way

    Assuming you want the time to display in 12hr format:

    1:15 PM
    1:15 AM
    11:30 PM
    11:30 AM

    Either comment out or delete these lines of the code:

     Case 5 ' e.g., 12345 = 1:23:45 NOT 12:03:45
                 TimeStr = Left(.Value, 1) & ":" & _
                 'Mid(.Value, 2, 2) & ":" & Right(.Value, 2)
     Case 6 ' e.g., 123456 = 12:34:56
                 TimeStr = Left(.Value, 2) & ":" & _
                 Mid(.Value, 3, 2) & ":" & Right(.Value, 2)
    Change this line of code:

    .Value = TimeValue(TimeStr)
    To:

    .Value = Format(TimeValue(TimeStr), "h:mm am/pm")
    Last edited by Tony Valko; 09-15-2013 at 09:37 AM.
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  2. #2
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Enter Date and Time quick way

    Here's the code with those changes applied. I also added a line that will clear the cell contents if an invalid time entry is made.

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
     Dim TimeStr As String
    
     On Error GoTo EndMacro
     If Application.Intersect(Target, Range("A1:A10")) Is Nothing Then
         Exit Sub
     End If
     If Target.Cells.Count > 1 Then
         Exit Sub
     End If
     If Target.Value = "" Then
         Exit Sub
     End If
    
     Application.EnableEvents = False
     With Target
     If .HasFormula = False Then
         Select Case Len(.Value)
             Case 1 ' e.g., 1 = 00:01 AM
                 TimeStr = "00:0" & .Value
             Case 2 ' e.g., 12 = 00:12 AM
                 TimeStr = "00:" & .Value
             Case 3 ' e.g., 735 = 7:35 AM
                 TimeStr = Left(.Value, 1) & ":" & _
                 Right(.Value, 2)
             Case 4 ' e.g., 1234 = 12:34
                 TimeStr = Left(.Value, 2) & ":" & _
                 Right(.Value, 2)
             Case Else
                 Err.Raise 0
         End Select
         .Value = Format(TimeValue(TimeStr), "h:mm am/pm")
     End If
     End With
     Application.EnableEvents = True
     Exit Sub
    EndMacro:
     Target.Value = ""
     MsgBox "You did not enter a valid time"
     Application.EnableEvents = True
     End Sub
    Change the range address to suit.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] automatically enter time & date
    By MUDDER460 in forum Excel General
    Replies: 18
    Last Post: 12-07-2012, 06:49 AM
  2. Macro to automatically enter time & date
    By mellis123 in forum Excel General
    Replies: 6
    Last Post: 06-16-2012, 06:44 AM
  3. Enter date and time
    By akhtar151 in forum Excel General
    Replies: 5
    Last Post: 10-13-2007, 11:25 AM
  4. [SOLVED] Any quick key for entering Current Date and Time?
    By Philip in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-12-2006, 12:25 PM
  5. [SOLVED] Any quick key to insert current Date and Time?
    By Philip in forum Excel General
    Replies: 2
    Last Post: 01-12-2006, 12:20 PM

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