Time Colon VBA

Brian from Maui

MrExcel MVP
Joined
Feb 16, 2002
Messages
8,459
Trying to revise a code to automatically add a colon and decimal point.

Cell is Custom Formatted as [mm]:ss.00

Want to just enter six digits and the colon and decimal point would be added with the code.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("timeCells")) Is Nothing Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
    With Target
    If Len(Target) = 6 Then .Value = Left(.Value, 2) & ":" & Mid(.Value, 2) & "." & Right(.Value, 2)
    Application.EnableEvents = True
    End With
End Sub

Obviuosly not not working. Can someone correct this? TIA
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
If you are asking how to bulletproof the code I suggested, try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim t As Range
    If Intersect(Target, Range("timeCells")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    On Error GoTo oops
    For Each t In Target
        t.Value = Format(t.Value, "0\:00\.00")
nexttarget:
    Next
    Application.EnableEvents = True
    Exit Sub
oops:
    Resume nexttarget
End Sub


Thanks, I'll have a play with this also..........
 
Upvote 0
Code:
    If Intersect(Target, Range("timeCells")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    On Error GoTo oops
    For Each t In Target
That code assumes that the entirety of Target is in timeCells if any cells are.
 
Upvote 0

Forum statistics

Threads
1,225,474
Messages
6,185,192
Members
453,282
Latest member
roger_nz66

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