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:
As already explained 'cause of the Len trap like you must read within its VBA help and it could happen with some other times …​

Thanks. I did a few more tests and fixed the code. It only happened on times of the day that have a decimal equivalent of just one digit, such as .7 or .8
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,214,795
Messages
6,121,624
Members
449,041
Latest member
Postman24

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