J
John Oliver
I had asked for a way to go to a cell, enter a value like "1234" and
have that displayed as "00:12:34". I was given the following snippet of
code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("E4:E27", "F4:F27")) Is Nothing Then Exit
Sub
On Error GoTo errHandler:
With Target
If IsNumeric(.Value) Then
Application.EnableEvents = False
Select Case .Value
Case 0
Case 1 To 99
.Value = TimeSerial(0, 0, .Value)
.NumberFormat = "hh:mm:ss"
Case 100 To 2359
.Value = TimeSerial(0, Int(.Value / 100), .Value Mod
100)
.NumberFormat = "hh:mm:ss"
Case 10000 To 235959
.Value = TimeSerial(Int(.Value / 10000), _
Int((.Value Mod 10000) / 100), .Value Mod 100)
.NumberFormat = "hh:mm:ss"
Case Else
End Select
End If
End With
errHandler:
Application.EnableEvents = True
End Sub
This mostly works, except it seems that if I enter a 4 digit number that
starts with a 3, 4, or 5, I get a weird date.
For example:
3124 gets me 7/20/1908 12:00:00 AM
4567 gets me 7/2/1912 12:00:00 AM
have that displayed as "00:12:34". I was given the following snippet of
code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Range("E4:E27", "F4:F27")) Is Nothing Then Exit
Sub
On Error GoTo errHandler:
With Target
If IsNumeric(.Value) Then
Application.EnableEvents = False
Select Case .Value
Case 0
Case 1 To 99
.Value = TimeSerial(0, 0, .Value)
.NumberFormat = "hh:mm:ss"
Case 100 To 2359
.Value = TimeSerial(0, Int(.Value / 100), .Value Mod
100)
.NumberFormat = "hh:mm:ss"
Case 10000 To 235959
.Value = TimeSerial(Int(.Value / 10000), _
Int((.Value Mod 10000) / 100), .Value Mod 100)
.NumberFormat = "hh:mm:ss"
Case Else
End Select
End If
End With
errHandler:
Application.EnableEvents = True
End Sub
This mostly works, except it seems that if I enter a 4 digit number that
starts with a 3, 4, or 5, I get a weird date.
For example:
3124 gets me 7/20/1908 12:00:00 AM
4567 gets me 7/2/1912 12:00:00 AM