24 Hour time formatting

DebbieW

New Member
Joined
Dec 21, 2005
Messages
8
Hi everyone. Can someone please let me know how I can type 2100 into a cell and have it automatically revert to 21:00. I will be entering "time" into cells in a 24hr format. eg, 2300, 1800, etc.

Thank you

Debbie
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi Debbie
Welcome to the board

To do this easily, you will need to use code
Assumming your times are being entered in column A (ie Col 1) you can use this code.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column <> 1 Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
    With Target
    .Value = Left(.Value, 2) & ":" & Right(.Value, 2)
    Application.EnableEvents = True
    End With
End Sub
This goes in the worksheet module. Right click sheet tab and choose view code. Paste in the panel on the right. Adjust colulm number as needed or use an intersect. Repost if you need help.

lenze
 
Upvote 0
Thank you Lenze for your very quick response. I have applied your code, but there is one small problem. I do not want the whole of the column to be formatted this way, I only want certain cells to work this way. They are random cells. I confirm that this formula is only applicable to one column, but as I said, not every cell in that column will need that formula. I hope I have made sense in how I have explained it. I would be very grateful if you can let me know if this can be done.

Debbie
 
Upvote 0
Hi Debbie:

I don't see Lenze around. So, let me take a look at it. One way to selectively translate your numeric entries into true time would be to have a unique qualifier, let us say the letter 'c' for convert as the fifth character of the entry that should be converted to true time ...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column <> 1 Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    Application.EnableEvents = False
    If Len(Target) = 5 And Right(Target, 1) = "c" Then
        With Target
        .Value = Left(.Value, 2) & ":" & Mid(.Value, 3, 2)
        End With
     End If
    Application.EnableEvents = True
End Sub
I hope this helps!
 
Upvote 0
You can still use my code via an INTERSECT statement. Select all the cells where the code is to apply. Use the control key to select non-contiguous cells. With these selected, Choose Insert>Name>Define. Enter a name (say timeCells) and click OK. Now change
If Target.Column <> 1 Then Exit Sub to

If Intersect(Target,Range("timeCells")) Is Nothing Then Exit Sub
Now the code will only execute when one of these cells is changed.

That said, there seems to be a problem with single digit hours. I'm checking on it.

lenze
 
Upvote 0
OK, Here's the fix
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) = 3 Then .Value = Left(.Value, 1) & ":" & Right(.Value, 2)
    If Len(Target) = 4 Then .Value = Left(.Value, 2) & ":" & Right(.Value, 2)
    Application.EnableEvents = True
    End With
End Sub

HTH

lenze
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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