use IF() with TIMEVALUE() in excel - excel

I have the folowing formula:
=IF(TIMEVALUE("14:30") - TIMEVALUE(NOW()) < TIMEVALUE("00:00"),"Past","Future")
Excel is giving me ERROR, i checked all formulas individually and they all give me the time value (which in theory should be enough to compare with an IF statement).
How come that i keep on getting error. Some cell formats not correct or something? Any help is appreciated!

Try to evaluate the formula step by step to see, if this is what you want.
Working formula:
=IF(TIMEVALUE("14:30") - NOW() < TIMEVALUE("00:00");"Past";"Future")
Evaluated arguments, using [F9 key]:
=IF(0,604166666666667 - 42719,6943635416 < 0;"Past";"Future")
You will always get the "Past" as returned value.
Explanation of date and time in Excel
The TIME in Excel is a proportion of a day. "00:00" = 0.00 and "24:00" = 1.00. Other values of TIME are DECIMALS between 0 and 1.
The DATE is a number of days since the first day. "1900-01-01" = 1 and "2000-01-01" = 36526. It is always an INTEGER.
Combining DATA and TIME (like in NOW() function) gives you an INTEGER + DECIMAL. When I evaluated the NOW() it returned 42719,6943635416
The TIMEVALUE function expects a STRING/TEXT that is considered a TIME and converts that STRING/TEXT to DECIMAL between <0; 1>.
Links
MS - How to use dates and times in Excel
Trump-Excel:Identify Errors Using Excel Formula Debugging (2 Methods)

Convert the Now to string first, using Text function, then it will work as expected.
TEXT(NOW(),"HH:MM:SS")
Final formula:
=IF((TIMEVALUE("11:30") - TIMEVALUE(TEXT(NOW(),"HH:MM:SS"))) < TIMEVALUE("00:00"),"Past","Future")

Related

VBA Date + TimeValue returns no time

I have a date and time which I assemble into a date + time from strings in the form
date_string = "2020-12-30" 'yyyy-mm-dd
date_code = CDate(date_string)
time_string = "00:00:00" 'hh:mm:ss
time_code = TimeValue(time_string)
date_time = date_code + time_code
Commonly the return looks like 05.01.2019 11:00:00, which is what I expect.
The returned values also all check out as TRUE if I test with IsDate(date_time)
Whenever the time is 00:00:00 however, I only get the date returned with no time appended. I dont quite understand this, since TimeValue(time_string)returns 00:00:00.
So it must be an issue when combining date and time to a date + time string.
Can someone please enlighten me why midnight somehow does no exist in Excel VBA or where my error in creating the time code is?
EDIT:
I try to explain my situation a bit better:
I do this date date/time stuff in code and then but the result in an array in a loop. Only later on it is written to a cell in a table.
By the time is is written into a cell, even custom formatting the cell to "DD.MM.YYYY hh:mm" does not show the time as it is completely missing from the cell value.
Do I neet to apply a format at the point of date_code + time_code?
Sometimes the answer can be so simple. Thanks to Variatus and Paul I checked formatting out.
I applied a date_time = Format(date_code + time_code, "dd.mm.yyyy hh:mm") in my code. Using this, my code runs as expected and 00:00:00 appears as expected, even in the cell values of the Excel table.
When you enter an integer, like 43930, in a cell Excel will record the number as an integer, just as you entered it. You can then proceed to format the cell as #,##0.000 and thereby make the number display as 43930.000. Or you can format that very same number as custom dd mmm yyy hh:mm:ss and display it as 09 Apr 2020 00:00:00. The point is that Excel chose to record the number in its most efficient way, as an integer.
So, if you enter a DateValue + TimeValue which, together, amount to an integer Excel will record the integer correctly. The format in which that integer is displayed in your worksheet is a matter for cell formatting.

Difference time view from Excel to VBA

I can't obtain the average time from start to end of some activities,
I tried 1K way but the result isn't correct, every time I've one day minus.
the image can explain better (that my english).
In this example the sum of my activities il 480:52:56 hours, in vba I've different result, for vba the date is "19/01/1900 00:52:56" like 456:52:56 hous
24 hours minus
why this difference? and how I can obtain the same result?
thanks for any suggestion
Dates are stored as serial numbers where first valid date has a value of 1. This value in excel reads as 01/01/1900, and in VBA as 31/12/1899. In excel, value 60 returns 29/02/1900 which doesn't exist in VBA, so from value 61 onwards all values will return the same date in VBA and excel.
/e: Also, maximum value is 2958465 (31/12/9999), values higher than that will return error rather than valid date
thanks to your comments I understand that the problem is for the minor dates of March 1, 1900 so I changed the select from:
Select [DataAttesa] as Data, avg(iif([totHours] > 1 and [totHours] < 61, dateadd("d",-1,CDate([totHours])) , [totHours])) as nr FROM [db_In$] Where TypeTrasp = "AOG" group by [DataAttesa] Order by [DataAttesa]asc
now, when I put the recordset.results on excel the value are correct.
Thank at all

VBA equivalent of DATEVALUE?

The formula function of =DATEVALUE and VBA's DateValue() provide exclusive results. How can I format a date in VBA to obtain the same serialization as I would by using =DATEVALUE?
Example: 1/19/2016, formatted "m/d/yyyy", will return 42388 through =DATEVALUE()
Use CLng e.g.
=DATEVALUE("7/7/16")
Returns
42558
In VBA:
? CLng(DateValue("7/7/16"))
42558
Several ways to do this. #Robin has a good one. I'll add one more
?DateDiff("d",0, "7/7/16")
42558
The DateDiff function calculates the difference between the two dates (2nd and 3rd parameters) in the units of the first parameter. So if you changed "d" to "h" you would get the number of hours between 0 (the first possible date in Excel/VBA) and 7/7/2016

Formula to compare time values

Below excel formula is working fine but in some cases its not give me proper value .
Input:
19:20:42
24:58:36
26:11:18
After using this formula:
=IF(TIMEVALUE(K7)>TIMEVALUE("09:00:00"),TRUE,FALSE)
I got the below output:
FALSE
TRUE
TRUE
What I Observe if the time value is > or = 24:00:00 it will not give me the proper answer.
How do I fix this?
As an alternative to Captain's excellent answer, you could also use:
=IF(K7>(9/24),TRUE,FALSE)
DateTime values are internally stored as a number of days since 1/1/1900,
therefore 1 = 1 day = 24 hours. So 9/24 = 0.375 = 9 hours :-)
You can easily verify this by clearing the format of your DateTime cells.
Edit: note that such Boolean formula can be expressed in a shorter way without losing legibility:
=K7>(9/24)
When you go over 24 hours, Excel counts it as the next day... and then the TIMEVALUE is the time the next day (i.e. 00:58:36 and 02:11:18 in your examples) and can, therefore, be before 0900.
You could do DATEVALUE(K7)+TIMEVALUE(K7) to ensure that you count the day part too...

Hours Calculations in Excel 2010

I have an Excel 2010 workbook which contains a cell with the value of, say, 9876:54:32 (manually entered) representing 9876 hours, 54 minutes and 32 seconds of, say, phone talk time.
Then I have a cell with the value of, say, 1000 (manually entered) representing 1000 calls.
I want to divide the values to get the average talk time of 592.615 minutes per call.
I'm doing a regular =A1/B1 and it gives me an error.
* EDITED *
Thanks Brain Webster for correcting my math. I mean 9.876 hours. But the point is that Excel gives me an error, not my manual math. Playing around with it I discovered that Excel is fine with me with values up to 9999:59:59. Once I try with 10000:00:00 and up, it doesn't recognize it as a time value.
I love these seemingly easy riddles, so here is my solution as a formula and as a VBA attempt:
my original:
= (LINKS(A38;FINDEN(":";A38)-1)/24)+ZEITWERT("0"&RECHTS(A38;LĂ„NGE(A38)-FINDEN(":";A38)+1))
translated:
= (LEFT(A38,FIND(":",A38)-1)/24)+TIMEVALUE("0"&RIGHT(A38,LEN(A38)-FIND(":",A38)+1))
This will get you the right value to a given 10k text of a time duration. You would only have to setup the format of the cell to [h]:mm:ss. Then those values will look the same, but one would be a string and the other a number - and that is a major difference ;)
In vba it looks much more easier, and once defined, you can use it as a worksheetfunction.
Public Function GetDurationValue(ByVal strInput As String) As Double
Dim arrResult As Variant
arrResult = Split(strInput, ":") 'saves you parsing
GetDurationValue = (arrResult(0) / 24) + _
TimeSerial(0, arrResult(1), arrResult(2))
End Function
A38 = "10971:12:14"
=GetDurationValue(A38)
=457.13349537037
You can use LEFT and RIGHT function to retreive parts of the time value and then sum and multiply these values by 60 [minutes] (resp. 3600 [hours]).
Something like this for the hours, minutes, seconds (A1 is the cell with time value):
B1=VALUE(LEFT(A1;FIND(":";A1)))*3600
B2=VALUE(LEFT(A1;FIND(":";A1; FIND(":";A1))))*60
B3=VALUE(LEFT(A1;FIND(":";A1; FIND(":";A1; FIND(":";A1)))))
Now you can sum that:
C1=SUM(B1;B2;B3)
Then divede by calls count (A2 is the cell with the calls count):
D1=C1/A2
Finally format it like time:
E1=TEXT(D1/(24*3600);"d \day\s hh:mm:ss")
BTW: I tried that in Excel 2013 and when I enter 111:22:33 into a cell it automatically converts to a time. So then I can divide it like you try...
It appears that hours > 10000 are not recognised as such by Excel. Therefore we need to introduce an IF() to see whether this is the case and determined the alternative formula for the case where hours >10000
=IF(ISERROR(FIND(":",A2)),A2/B2, <SCRIPT IN CASE OF >10000>)
<SCRIPT IN CASE OF >10000> will now be:
VALUE(LEFT(A2,FIND(":",A2)))/24+VALUE(LEFT(A2,FIND(":",A2, FIND(":",A2))))/(24*60)+VALUE(LEFT(A2,FIND(":",A2, FIND(":",A2,FIND(":",A2)))))*(24*60*60)
combine and enjoy!
Assuming you don't exceed 100,000 hours in A1, and you always display hours, minutes and seconds then this formula should suffice
=IFERROR(A1/B1,(LEFT(A1)*10000/24+RIGHT(A1,10))/B1)
format result cell as [h]:mm:ss to get the result as a time value
With 10971:12:14 in A1 and 1000 in B1 that should give a result of 10:58:16 [or format result cell as [m]:ss to get minutes and seconds like 658:16]
This version will work with any number of hours and with or without seconds
=IFERROR(A1/B1,(LEFT(A1,FIND(":",A1)-1)/24+RIGHT(A1&IF(COUNTIF(A1,":*:"),"",":00"),5)/60)/B1)

Resources