Entering the time in a cell

H

Hezzy

How do it enter in a single digit for time but have it displayed at full
time. For example if I type in the number 8 how to I get it to display 8:00?
 
L

Luke M

It won't be a true time value, but you can get the appearance by formatting
cell:
0":00"
 
S

Shane Devenshire

Hi,

Here is some rather more complex code which you can cut down to what you need:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim I As Integer
Set isect = Application.Intersect(Target, Range("Time"))
If isect Is Nothing Then Exit Sub 'not inside the desired range
If Target.Count > 1 Then Exit Sub 'more than one cell selected
Application.EnableEvents = False
'Numeric entries
If Target >= 1 Then
If Len(Target) = 1 Then
Target = Target & " AM"
ElseIf Len(Target) = 2 Then
If Target <= 12 Then
Target = Target & " AM"
Else
Target = Target - 12 & " PM"
End If
ElseIf Len(Target) = 3 Then
Target = Left(Target, 1) & ":" & Right(Target, 2)
Application.EnableEvents = True
Exit Sub
ElseIf Len(Target) = 4 Then
Target = Format(Left(Target, 2) & ":" & Right(Target, 2),
"HH:MM AM/PM")
Application.EnableEvents = True
Exit Sub
Else
MsgBox "That entry is not a legal time."
Target.ClearContents
Application.EnableEvents = True
Exit Sub
End If
End If
End If
Application.EnableEvents = True
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top