Converting minutes and seconds to perform calculations

A

aquaticdoc

Version: 2008
Operating System: Mac OS X 10.6 (Snow Leopard)
Processor: Intel

I have a large spreadsheet with elapsed time records shown as 53.23 sec, and others as 423.04, meaning 4' 23.04 sec, and need to perform calculations on these. I have tried formatting with the time formats, but none give me results that make any sense for this use. For example, I would like to know what percentage of 53 sec is 4 min 23 sec. The prior suggestions on this topic have not worked.
 
J

John McGhie

One of the first things we learned about computing was "If you can't find
the answer, change the question!"

What that means is: in computing your question needs to be literally correct
or the computer can't answer it.

In your case, you are asking the computer to convert a "something" that is
not a time. The previous answers on this topic would have worked fine to
convert a "Time". But chances are, that's not what you have in those cells.
So the suggestions "work", but they are not the answer to YOUR question :)

Excel stores "time" as a "decimal fraction of a day". The part to the LEFT
of the decimal is "The number of days since January 2, 1904" on the Mac, and
the number to the right of the decimal is "the fraction of the day between
midnight and this time".

To perform computation on those numbers, you must first convert them into
Excel times. Sadly, the brick wall you hit was that the Excel time function
will not produce precision greater than tenths of a second. Which means we
have to convert those times in seconds and decimals to "decimal fractions of
a day" the hard way.

Of course, in other versions of Excel, we can leap in with a little VBA
function that does this easily and without a lot of thought or effort. But
in Excel 2008, we have to do it the really hard way :)

Add a column beside the column that shows 53.23 sec and type in it
=C3/(24*60*60)

Assuming the value in C3 is a number of seconds and decimals of a second,
that will produce a number that will format as a Time in Excel. If you give
the result cell a custom format of "mm:ss.000" you will see

63.646400 seconds represented as 01:03.646

The 423.04 is a similar challenge. Again, we have to "roll our own" time,
because Excel's Time function does not offer the precision we need.

Assume that Cell C3 contains "462.5300". Note that I have expanded the
number format to show four decimal places, so we can make sure we do not
drop any due to rounding errors.

Here's the formula:

=TIME(0,VALUE(LEFT($C3,1)),0)+TIME(0,0,MID($C3,2,2))+IF(ISERR(VALUE(RIGHT($C
3,LEN(C3)-3))),0,VALUE(RIGHT($C3,LEN(C3)-3))/(24*60*60))

Simple? Let's parse it out :)

=TIME(0,VALUE(LEFT($C3,1)),0)

This bit gets the left-most character of the string (the "4" in 423.04) and
converts it to an Excel Time value. We don't need the precision here,
because we can never have more than 60 minutes.

+TIME(0,0,MID($C3,2,2))

This bit grabs the two seconds digits and adds them into the mix, as
seconds.

+IF(ISERR(VALUE(RIGHT($C3,LEN(C3)-3))),0,

This bit checks for an error if we attempt to get a value from the string to
the right of the decimal point. If we do get an error, chances are that
string contains "0000" so we simply set the value to 0, otherwise...

VALUE(RIGHT($C3,LEN(C3)-3))/(24*60*60))

We grab the characters to the right of the decimal point, convert them to
hundredths of seconds, and simply add them to the Time value we are
building.

That will do it :)

Cheers



Version: 2008
Operating System: Mac OS X 10.6 (Snow Leopard)
Processor: Intel

I have a large spreadsheet with elapsed time records shown as 53.23 sec, and
others as 423.04, meaning 4' 23.04 sec, and need to perform calculations on
these. I have tried formatting with the time formats, but none give me results
that make any sense for this use. For example, I would like to know what
percentage of 53 sec is 4 min 23 sec. The prior suggestions on this topic have
not worked.

This email is my business email -- Please do not email me about forum
matters unless you intend to pay!

--

John McGhie, Microsoft MVP (Word, Mac Word), Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. | Ph: +61 (0)4 1209 1410
+61 4 1209 1410, mailto:[email protected]
 
A

aquaticdoc

Thank you so much for the e-mail, John. I still have Excel 2004, which does
allow VBA, I think. Unfortunately, the spreadsheet that was sent to me has
the times in a fairly basic format: minutes are shown as all characters 3
digits to the left of the decimal point, so 1147.23 would be 11 minutes,
47.23sec. My frustrating workaround on the pieces that I've done so far have
been to convert 11 min. into seconds, so the entry would be 707.23, and I
just left it formatted as a general rather than time.

I suppose there is a way to make the computer assume that if there are
digits beyond 2 to the left of the decimal, convert those numbers by
multiplying by 60 and add the 2 to the left of the decimal. But that
requires better programming skills than I possess.

Thanks again,
Bruce
 
J

John McGhie

Yes, Excel 2004 does do VBA, and there are several nice routines for
converting strings into times if you Google around for them.

You can make Excel work out what shape and size a string is. I did one of
those for you in the answer I sent:

VALUE(RIGHT($C3,LEN(C3)-3))

Look up the LEN, and LEFT, RIGHT and MID string functions in the help.

But what they have sent you, in Spreadsheet terms, is not "a basic format",
it's a "mess" :) Because they have mixed three kinds of data in the same
cell, without distinguishing between them. They have minutes, seconds, and
decimal fractions of seconds, all piled in together.

Yes, I guess you could just convert everything into seconds. I don't know
your data or what you need to do with it. I can think of some things you
might not be able to do if everything is converted to a number of seconds.

If you just "multiply by 60": that does not convert into "seconds", it
converts into "the number of seconds". To do arithmetic on it in Excel, you
usually need to convert further, into a "Time".

An Excel time has a number of days to the left of the decimal, and the
decimal fraction of a day to the right. To Excel, a time of 456.09 is
0.003426967593 of a day.

Once you have these in Excel's time format you can do anything you like with
them: add, subtract, compare...

It is not safe to allow a computer to "assume" anything :) If you do not
know how may characters you have to the left of the decimal point, count
them. Your life will be happier :)

Some of these formulae can get fairly hairy. I never try to get them all
working at once in a single cell. I make several cells and get the
individual pieces working, then I join them all together.

Cheers



Thank you so much for the e-mail, John. I still have Excel 2004, which does
allow VBA, I think. Unfortunately, the spreadsheet that was sent to me has
the times in a fairly basic format: minutes are shown as all characters 3
digits to the left of the decimal point, so 1147.23 would be 11 minutes,
47.23sec. My frustrating workaround on the pieces that I've done so far have
been to convert 11 min. into seconds, so the entry would be 707.23, and I
just left it formatted as a general rather than time.

I suppose there is a way to make the computer assume that if there are
digits beyond 2 to the left of the decimal, convert those numbers by
multiplying by 60 and add the 2 to the left of the decimal. But that
requires better programming skills than I possess.

Thanks again,
Bruce

This email is my business email -- Please do not email me about forum
matters unless you intend to pay!

--

John McGhie, Microsoft MVP (Word, Mac Word), Consultant Technical Writer,
McGhie Information Engineering Pty Ltd
Sydney, Australia. | Ph: +61 (0)4 1209 1410
+61 4 1209 1410, mailto:[email protected]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top