I am preparing a workbook that requires the user to input time values. I don't want to restrict the user to using just one format, they should be able to enter values as hh:mm:ss or as decimal values. The idea is to automatically detect values formatted as time (which Excel does automatically if entered in a hh:mm:ss format) and convert them to decimal form. Since decimal form is the goal format, it should not be touched. There are no addition or subtraction operations issues to deal with afterward, they only get ordered and used in calculating a function of time. Please note that I want to avoid using VBA code.
I have found the following resource that gets me close to the desired result. Since I don't have many posts, you will have to use your intelligence to decipher the URL:
excelribbon [dot] tips [dot] net/T009699_Checking_for_Time_Input
That article offers two approaches. In the first approach, it uses CELL with "format" for info_type. The returned value should lie in the range D6 to D9, the usual values for time formats.
The second approach converts the value using TEXT and a format code of "hh:mm:ss", converting back using TIMEVALUE and then checking whether the result is the same as the original cell.
Both of these give the same results for non-zero values, but there is an issue. The methods do not recognize time formats when the time is greater than 24 hours. As the article states, "The function works fine as long as [the source] cell contains only a time. If the cell contains both a date and time, then the function always returns 'Not a Time Entry.'" When a time is entered that is greater than 24 hours (24:00:00 or more), Excel apparently converts it to date-time format, i.e., the number format is changed to [h]:mm:ss and a date is added when viewed in the formula bar (e.g., 58:49:02 is represented in the formula bar as 1900-01-02 10:49:02). I am looking either for a way to modify the test so that it can identify this case as well, or for an alternative test method. So feel free to ignore my comments here and swoop in with a completely different solution.
I have played around with the formulas in the attached workbook, which demonstrates the issue. My only success has been a somewhat better understanding. First for the formula above using CELL and "format". Take B2 as the original cell, and use "TE" for "Time entry" or "Not TE" for "Not a time entry." If I enter a decimal value, the value returned for CELL("format",B2) is G. If I enter a time, such as 6:04 or 6:04:00, Excel returns D9 and D8, respectively. If I enter 58:49:02 as above, it again returns G for general. The result of the test is therefore wrong (returns "Not TE", although for my purposes it should be "TE").
For the second formula using TEXT and TIMEVALUE, I tried to understand how Excel interprets these, and if they could be modified. When using TEXT(B2,"hh:mm:ss"), Excel expresses integers as zero, time values < 24h as time values, time values >= 24h as the remainder of the time value after removing the day value, and for decimals I believe it interprets them as the decimal format underlying the date-time system. Decimals are not a problem, since they still get detected as "Not TE," so an accurate interpretation is not needed. The real problem is time values >= 24h.
I tried using the formula with "[h]:mm:ss" instead of "hh:mm:ss", that is:
While the TEXT does get interpreted as an actual duration for values longer than 24h, converting it back using TIMEVALUE still results in a detection of "Not TE." Worse, once a decimal exceeds the value of approximately 416.6666... the TIMEVALUE returns "#VALUE!", since the value exceeds 10,000 hours. Using hh:mm:ss avoids this issue.
Bookmarks