Time quirk

normpam

Active Member
Joined
Oct 30, 2002
Messages
355
The code below works just fine. If someone enters '915' they will get 9:15am, or '1345' and get 1:30pm. But it seems two times never work.... 12:00pm which results in 0:.5 and
1648 which results in 0:.7


Any ideas?

VBA Code:
Sub Auto_Open()
    Call SetUp_OnEntry
End Sub

Sub SetUp_OnEntry()
    Application.OnEntry = "SetColon"
End Sub


Sub SetColon()
    If ActiveCell.Column = 3 Or ActiveCell.Column = 2 Or ActiveCell.Column = 4 Or ActiveCell.Column = 5 Then
    If Len(ActiveCell.Value) = 4 Then ActiveCell.Value = Left(ActiveCell.Value, 2) & ":" & Right(ActiveCell.Value, 2)
    If Len(ActiveCell.Value) = 3 Then ActiveCell.Value = Left(ActiveCell.Value, 1) & ":" & Right(ActiveCell.Value, 2)
    End If
End Sub
 
Last edited by a moderator:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
those values work perfectly Ok with my version of excel. I was testing it using this code:
VBA Code:
Sub SetColon()
If Len(ActiveCell.Value) = 4 Then tt = Left(ActiveCell.Value, 2) & ":" & Right(ActiveCell.Value, 2)
If Len(ActiveCell.Value) = 3 Then tt = Left(ActiveCell.Value, 1) & ":" & Right(ActiveCell.Value, 2)
ActiveCell.Offset(1, 0) = tt
MsgBox "tt " & tt

End Sub
Your reference to application.onentry is something that has been superceded by the worksheetchange event , so I am wondering what version of EXCEL you are using, can you update your profile to specify which version of excel you are using because the anwer may depend on which verson of excel you have
 
Upvote 0
I was also using Excel over 20 years ago, but I can't remember all the quirks about the syntax of those old versions, so unless you are are still using Excel 97 or something, what about telling what version you are using by updating your profile. Then somebody might be able to suggest a rewrite to use some new millenium techonology!! It might solve your problem!! ( Actually if you are using Excel 97, this is even more important!!)
 
Upvote 0
Whatever the Excel version it should be like​
VBA Code:
Sub SetColon()
        Dim L&
    With ActiveCell
        L = Len(.Value)
        If .Column > 1 And .Column < 6 And IsNumeric(.Value) And L > 2 And L < 5 Then _
           .Value = Left(.Value, L - 2) & ":" & Right(.Value, 2)
    End With
End Sub
 
Upvote 0
As the glitch occurs when the procedure is launched on a cell already formatted as time so this is the update :​
Rich (BB code):
Sub SetColon()
        Dim L&
    With ActiveCell
        L = Len(.Value)
        If .Column > 1 And .Column < 6 And IsNumeric(.Text) And L > 2 And L < 5 Then _
           .Value = Left(.Value, L - 2) & ":" & Right(.Value, 2)
    End With
End Sub
According to Excel behavior / rules with a cell already formatted as time (date) the user must enter the colon character …​
 
Last edited:
Upvote 0
The good way to avoid the glitch (Len trap !) if the cell is already formatted as time :​
Rich (BB code):
Sub SetColon()
        Dim L&
    With ActiveCell
        L = Len(.Text)
        If .Column > 1 And .Column < 6 And IsNumeric(.Value) And L > 2 And L < 5 Then _
           .Value = Left(.Value, L - 2) & ":" & Right(.Value, 2)
    End With
End Sub

 
Upvote 0
The good way to avoid the glitch (Len trap !) if the cell is already formatted as time :​
Rich (BB code):
Sub SetColon()
        Dim L&
    With ActiveCell
        L = Len(.Text)
        If .Column > 1 And .Column < 6 And IsNumeric(.Value) And L > 2 And L < 5 Then _
           .Value = Left(.Value, L - 2) & ":" & Right(.Value, 2)
    End With
End Sub

Thanks.... will need to study this a bit...
 
Upvote 0
As already explained 'cause of the Len trap like you must read within its VBA help and it could happen with some other times …​
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,214
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top