Converting Number to Time

Claymationator

Well-known Member
Joined
Sep 26, 2006
Messages
705
I am trying to compile data from several different managers within my company. They all have similar workbooks, and have been entering time in a number format. Currently they have values that look like this:
Book1
ABCD
11.351.18
22.012.23
32.333.05
44.124.22
Sheet1


However, the data should appear as this:
Book1
DEFG
101:3501:18
202:0102:23
302:3303:05
404:1204:22
Sheet1


So, in any instance the period or decimal point should be a colon and the format of the cells should be in [m]:ss.

I want to be able to select a range within the workbook and run a macro that will convert the numbers into the "[m]:ss format.

Any help is appreciated.

Thanks.
 
Hi Clayton,
Conversion VBA-tool could be like this:
Rich (BB code):
<font face=Courier New>
' Safe conversion from hh.mm double to hh:mm time
' Only numerical cells in the chosen range(s) will be converted
Sub Num2Time()
  Dim x As Range
  On Error Resume Next
  For Each x In Application.InputBox(Prompt:="Range for conversion", _
                            Title:="Num2Time", _
                            Default:=Selection.Address(0, 0), _
                            Type:=8)
                            
      If Not IsEmpty(x) And IsNumeric(x) And (x.NumberFormat <> "[h]:mm") Then
        x.Value = Int(x) / 24 + (x - Int(x)) * 100 / 1440
        x.NumberFormat = "[h]:mm"
      End If
      
  Next
End Sub
</FONT>
Regards,
Vladimir
 
Last edited:
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
xld,

Thanks for the post, that seems to do the trick. I didn't see your post before, I just went straight to the second page of the thread. Thanks again.

Clay
 
Upvote 0
Thanks Vladimir,

However, how would that code change since all of my times are in [m]:ss?
The code for conversion of the double to [m]:ss would be like this:
Rich (BB code):
<font face=Courier New>
Sub Num2Time()
  Dim x As Range
  On Error Resume Next
  For Each x In Application.InputBox(Prompt:="Range for conversion", _
                            Title:="Num2Time", _
                            Default:=Selection.Address(0, 0), _
                            Type:=8)
                            
      If Not IsEmpty(x) And IsNumeric(x) And (x.NumberFormat <> "[m]:ss") Then
        x.Value = Int(x) / 1440 + (x - Int(x)) / 864  ' 100 / 86400 =  1 / 864
        x.NumberFormat = "[m]:ss"
      End If
      
  Next
End Sub</FONT>
Vladimir
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,346
Members
452,638
Latest member
Oluwabukunmi

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