How to convert/typecast a time string?

richard.ca

Board Regular
Joined
Nov 7, 2007
Messages
100
Can't quite get my head around how Excel stores and manipulates time values.
Here's my problem. I'm doing some time arithmetic and the time values are read
in from a file in "hh:mm:ss" format. Initially I was writing this directly to a cell and
then making a temporary copy for use later, code goes something like this:

Dim off_time As Double

off_time = Cells(row, n).Value

This worked but there was a performance impact since it was repeated many times in a
large loop. To speed things up I want to read the string time value into a code variable
instead of into a cell, like this:

Dim time_str as string

' load time into time_str etc

Now I have a problem because if I try to assign off_time = time_str of course I get a
run-time error due to imcompatible types. Excel obviously does some implicit type
conversion in the case of off_time = Cells(row, n).Value. How can I convert time_str
into the internal Excel time representation. I want to say something like:

off_time = convert_time(time_str)

but haven't found any suitable built-in function.

I would be grateful for any suggestions.

Many thanks,

Richard.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Richard

If you are looking for a function how about TimeValue?
 
Upvote 0
I knew there had to be a really simple solution - couldn't see the wood for the trees!

Thanks a lot Norie!
 
Upvote 0

Forum statistics

Threads
1,225,623
Messages
6,186,063
Members
453,336
Latest member
Excelnoob223

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