Convert four digits to time upon data entry

Biking Loki

Board Regular
Joined
Aug 25, 2005
Messages
167
I've seen a few thread about how convert an adjacent cell to xx:xx time format. However, is there a way to format the cell so that when I type '1330' and press enter, it will automatically convert to 13:30?
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
If 1330 is 13 hours 30 minutes then

=TEXT(A1,"00\:00\")*1

If 1330 is 13 Minutes 30 Seconds then

=TEXT(A1,"00\:00\:00")*1


Hope that helps.
 
Upvote 0
If 1330 is 13 hours 30 minutes then

=TEXT(A1,"00\:00\")*1

If 1330 is 13 Minutes 30 Seconds then

=TEXT(A1,"00\:00\:00")*1


Hope that helps.

But doesn't that apply to an adjacent cell? How can I make it apply to the cell I am currently in? I want it to convert cell "on the fly" if you will.
 
Upvote 0
That code is more complicated than it needs to be:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range, d As Range
Set d = Intersect(Target, Range("A:A"))
If d Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each c In d
    If IsNumeric(c) And c <> "" And Not c.HasFormula Then _
        c = TimeValue(Format(c, "00\:00"))
        c.NumberFormat = "h:mm"
Next
Application.EnableEvents = True
End Sub
 
Upvote 0
1. If you want to have this conversion happen on one worksheet, right-click its tab on the bottm, chooce View Code, and paste this into the VBE window:
Code:
Option Explicit
 
Private Sub Worksheet_Change(ByVal Target As Range)
 
    If Target.Cells.Count > 1 Then Exit Sub
    If Not (Target.Formula Like "####") Then Exit Sub
 
    Target.Value = Date + TimeSerial(Left$(Target.Formula, 2), Right$(Target.Formula, 2), 0)
    Target.NumberFormat = "hh:mm"
 
End Sub

2. If you want this conversion to happen throughout the workbook, right-click the sheet tab and choose View Code, but then hit Ctrl-R in the VBE to open the Properties window, and double-click "ThisWorkbook" to get the workbook's code, not the sheet's code. Check the VBE window's title bar, and use the Window menu to switch if you need to. In ThisWorkbook's code, paste this, which differs only in the top line:

Code:
Option Explicit
 
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
 
    If Target.Cells.Count > 1 Then Exit Sub
    If Not (Target.Formula Like "####") Then Exit Sub
 
    Target.Value = Date + TimeSerial(Left$(Target.Formula, 2), Right$(Target.Formula, 2), 0)
    Target.NumberFormat = "hh:mm"
 
End Sub
 
Upvote 0
1. If you want to have this conversion happen on one worksheet, right-click its tab on the bottm, chooce View Code, and paste this into the VBE window:
Code:
Option Explicit
 
Private Sub Worksheet_Change(ByVal Target As Range)
 
    If Target.Cells.Count > 1 Then Exit Sub
    If Not (Target.Formula Like "####") Then Exit Sub
 
    Target.Value = Date + TimeSerial(Left$(Target.Formula, 2), Right$(Target.Formula, 2), 0)
    Target.NumberFormat = "hh:mm"
 
End Sub

That worked, but now I traded one problem for another. The formula I had at the bottom to add the column of times doesn't work now.

I had a formula to add the columns, for example
A1 = 08:00
A2 = 12:00
A3 = 13:00
A4 = 17:00

The formula is "=(A4-A3)+(A2-A1)" and it gave me the total work hours for a given day. However, after pasting in the code, the formula answer is nothing but number signs. Is there a way to fix that or a better formula/method to add and track the time?
 
Upvote 0
One other thing I noticed is that when I enter the time with a leading 0, the time entered changes to 0:00. Example 0750 turns into 0:00. Also, 750 turns into 0:00.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

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