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.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Code:
Public Sub ChangeTimes()
Dim cell As Range
  
    For Each cell In Selection
    
        cell.Value = Replace(cell.Value, ".", ":")
        cell.Value = cell.Value / 60
        cell.NumberFormat = "[m]:ss"
    Next cell
End Sub
 
Last edited:
Upvote 0
Thanks, that just about does it. However, there are a few instances where it doesn't seem to work just right in the conversion. That is when the initial value ends in a "0".

For intance:

0.50 changes to 0:05 instead of 0:50
1.50 changes to 1:05 instead of 1:50

Is there a way around that?
 
Upvote 0
You don't need to use VBA. Just use the text to columns feature to seperate the data (use period as the delimiter) and then join the two back to gether with concatination. i.e. =TEXT(A2&":"&B2,"h:mm")
 
Upvote 0
You don't need to use VBA. Just use the text to columns feature to seperate the data (use period as the delimiter) and then join the two back to gether with concatination. i.e. =TEXT(A2&":"&B2,"h:mm")

Or a formula

=TIME(0,INT(A1),MOD(A1,1)*100)

Thanks for those responses. I have been using a similar formula, but the way that the spreadsheet are set up make the formulas very time consuming, where VBA would be a quicker process.
 
Upvote 0
Why not use the current formula you are using in VBA then, either using the evaluate method or using the worksheetfunction code within the VBA?
 
Upvote 0
Why not use the current formula you are using in VBA then, either using the evaluate method or using the worksheetfunction code within the VBA?

schielrn,

Thanks for the post. Could you possibly get me started on that? I haven't used the evaluate or worksheetfunction modes before. Jonmo's formula is a little neater than mine was, so I have changed the formula to:

=TIME(0,INT(A1),MOD(A1,1)*100)
 
Upvote 0
Maybe try:

Code:
sub test()
for each c in range("a1:b4")
c.value = worksheetfunction.text(Evaluate("TIME(0,INT(" & c.Value & "),MOD(" & c.Value & ",1)*100)"), "[m]:ss")
next c
end sub
Hope that helps. Modify as needed. You may need to get rid of the worksheetfunction.text part and just let them come in as decimals and then format separately if you are doing calculations because it may be stored as h:mm and not actually m:ss?
 
Upvote 0
Thanks, that just about does it. However, there are a few instances where it doesn't seem to work just right in the conversion. That is when the initial value ends in a "0".

For intance:

0.50 changes to 0:05 instead of 0:50
1.50 changes to 1:05 instead of 1:50

Is there a way around that?

Code:
Public Sub ChangeTimes()
Dim cell As Range
  
    For Each cell In Selection
    
        cell.Value = Replace(cell.Text, ".", ":")
        cell.Value = cell.Value / 60
        cell.NumberFormat = "[m]:ss"
    Next cell
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,272
Messages
6,129,822
Members
449,538
Latest member
cookie2956

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