I have the following Excel code:
=IF(K4<=31/03/2014,TRUE,FALSE)
K4 contains a date:
01/01/2014
Yes, the code brings back FALSE, I have also tried =IF(K4<="31/03/2014",TRUE,FALSE) but with the same result.
Any ideas or suggestions as to where I am going wrong?
Use
=IF(K4<=DATE(2014,3,31),TRUE,FALSE)
You are testing an inequality with a Float <= String, which returns FALSE. The DATE(,,) fx gives you the required Float for equality comparison.
Alternatively, =K4<=DATE(2014,3,31) is a shorter way of achieving the same.
What K4<=31/03/2014 evaluates to is:
K4 <= (31/03)/2014
31/03 gives 10.333... and that divided by 2014 gives 0.005131... which is why it will always evaluate to false (01/01/2014 is equivalent to 41640, i.e. the number of days since '00/01/1900')
I don't know if it's a system thing, but =IF(K4<="31/03/2014",TRUE,FALSE) gives me TRUE, but then, so does =IF(K4<="31/03/2013",TRUE,FALSE).
A quick and dirty trick would be to multiply the string by 1:
=IF(K4<="31/03/2014"*1,TRUE,FALSE)
The multiplication tells excel to convert the string to a number, if it is parsable (You can also use any other numerical operation that doesn't change the value itself, such as +0).
Note: Excel dates and numbers are the same. Dates are just formatted differently so that a number with a specific format can appear as a date, time, or any other format you can have.
Also, if you really are using this IF construct, you are better of simply using the comparator:
=K4<="31/03/2014"*1
I had the same problem and found that excel was not recognizing one of my 2 columns as a date. (Try changing the date format to 14-Mar-12 to see if this is the case for you)
Related
I am using VBA to compare two times from a spreadsheet.
Im my example the actual value form the sheet is 23:00 in two cells. I use them for an if-statement, the values both come from two different arrays from the type Variant.
if dataArr(v1) = rowArr(v2) Then
If I debug the two values it shows the two value like this
0,958333333333333 / 0,958333333333333
They seem the same but the compare in the if-statement returns false
So I subtracted the two value to see if the result is zero.
dataArr(v1) - rowArr(v2)
In this case the result is not zero but
3,33066907387547E-16
So something is wrong with the compare or the two double values that represent the time.
The only working solution I found was to convert them to string values by
CStr(dataArr(v1)) = CStr(rowArr(v2))
which returns true as it should be.
I don't really like this solution because it is not really how it should work. I also investigated what happens to the values before they end up in the compare but I could not find any mistake . Actually all the values in the dataArr are coming from the spreadsheet (ListObject.DataBodyRange.Value) and then certain values are copied to the rowArr out of the dataArr so it really should be the same value.
Any suggestions? Thanks!
If the cells are actually formatted for time instead of a string, I would make the definition of my arrays a "Date" not a "Variant".
Then you would have all the date/time related functionality in VBA at your disposal (like DateDiff() ).
Sometimes floating point inaccuracies creep in. If you want to compare then take the Absolute Value (ABS) of the subtraction and see if it is below a tolerance level, say, 1E-7 (a millionth).
Thanks to Florent B I got a solution that works!
Florent B: To compare the equality of two dates, you need to round to the precision which is 24hrs x 60min x 60 sec
So the solution is:
If Round(dataArr(v1) * 86400) = Round(rowArr(v2) * 86400) Then
It makes perfektly sense. Thanks a lot!
for some reason my excel drops error when multyipling on if statemtn
for example
=IF(A1>B1;A1*5;A1)
I cant do nothing, if Id skip it as 5x5 for exmaple, than it works perfect, though If I use an cell for a multiply, it drops me Value error.
I examined it and calculation steps are:
=IF(TRUE;"20.00"*5;A2), the 20.00 is what is A2 cell.
=IF(TRUE;#VALUE!;A2)
So I am stuck and broken...
This is strange. When multiplying a string that looks like a number, Excel normally automatically converts the string to a number. Try this to force conversion:
=IF(A1>B1;VALUE(A1)*5;A1)
If that still results in an error, then Excel can't recognize "20.00" as a number. Does your locality use a comma instead of a fullstop for the decimal places? If so, try putting 20,00 in A1.
Also, try checking your locality settings. From the Start menu, search for Locality or Language, then choose to change your Date, Time or Number Formats. Click Additional settings and check what your Decimal Symbol is. You need to use this decimal symbol in Excel for it to correctly recognize numbers.
Please use this formula,
=IF(A1>B1,PRODUCT(A1,5),A1)
I have an issue that I don't personally know how to format. I need to subtract numbers that are in seconds, formatted to be viewed as 58.43 or 59.99, but that are sometimes in minutes, formatted as 1:01.33 for example.
I would also need to be able to subtract the numbers from each other to be recognized as (+1.08) or (-0.78), with the parentheses.
I'm sure I can elaborate somewhere, so let me know if this doesn't make any sense. Thanks
It depends if 58.43 is formatted as a number or time. Date and time are stored in number of days, so the time 58.43 is actually stored as the number 0.00067627314814814800000 (58.43/24/60/60).
If both values are time values, then the custom number format of the result can be:
(+s.00);(-s.00);(0.00);#
To handle both cases, instead of =A1-A2 you can try this something like this:
=IF(A1<1,A1,A1/86400)-IF(A2<1,A2,A2/86400)
If those are just time values formatted as mm.ss then you can use TIMEDIFF()
https://support.office.com/en-us/article/Calculate-the-difference-between-two-times-e1c78778-749b-49a3-b13e-737715505ff6
If not, try to convert them to time values and than use TIMEDIFF()
The first part is straightforward
Apply a default format of
ss:00
Then in conditional formatting use a formula
=A2>=TIME(0,1,0)
and apply a format of
m:ss.00
for the ones that are a minute or more.
There is no direct solution to the problem of displaying negative times short of changing the default date system used by Excel as you can see in a number of references. The only way to do it here is to test whether the result is positive or negative and display the positive difference with or without a minus sign.
=IF(B2>=A2,TEXT(B2-A2,"(+s.00)"),TEXT(A2-B2,"(-s.00)"))
The downside of this is that they are actually text values and you can't use them in any further calculations. However the results of A2-B2 are still good even if you can't display them directly, so you can use A2-B2 in subsequent formulae if you want to even if it is negative.
I have a working formula for the following text to date conversion. However, I don't understand why the trailing 0 has to be added in order to show the year in YYYY format.
21.04.2016 converts to 4/21/2016
=(MID(A2,4,2)&"/"&LEFT(A2,2)&"/"&RIGHT(A2,2))+0
It's very simple and straight-forward. However if you remove the 0 at the end of the formula, it will only show 16 instead of 2016. But I could do Right(A2,4) instead of Right(A2,2). But I still like to know why? Anyone? Thanks!
The trailing zero turns the whole thing into a math operation which causes the string (everything to the left of the +0) to be treated as a number.
you could also use *1 instead of +0
=(MID(A2,4,2)&"/"&LEFT(A2,2)&"/"&RIGHT(A2,2))*1
or you could drop the +0 and at the front add -- before the ( and it should all do the same.
=--(MID(A2,4,2)&"/"&LEFT(A2,2)&"/"&RIGHT(A2,2))
As Ed has correctly answered, this is because it is treating your date string as a number.
However, Excel has to interpret the string to get what number it really is, and to do this it relies on regional settings.
Under US regional settings, your formula works great, but when I plug it into excel with UK regional settings I get #Value because "04/21/16" isn't a valid date or number in the UK.
In order to avoid this problem, you should convert it to a date using the DATE() function, which will work irrespective of your regional settings.
=DATE(RIGHT(A2,4),MID(A2,4,2),LEFT(A2,2))
I am currently trying to convert yyyymmdd type of date to a ddmmyyy format.
I've tried using DATE function and something like this:
=DATE(LEFT(A3;4);MID(A3;5;3);RIGHT(A3;7))
Original date for this function is 20120401 but the formula returns: 16.12.2104.
I've tried using functions YEAR, MONTH and DAY instead of LEFT, MID and RIGHT but that's not the right way.
I also tried using DATEVALUE but since Excel probably doesn't recognize yyyymmdd as a date, it gives me a #VALUE! error.
I've found a couple of solutions for SQL but it isn't my strong side yet - and this should be achievable easily in Excel.
Is there a way to do this in Excel?
Applying =DATE(LEFT(A3;4);MID(A3;5;3);RIGHT(A3;7)) to 20120401 in A3 is effectively to reassemble the component characters as below on the left:
These effectively become converted as on the right.
The solution was simply to exclude the digits highlighted red with:
=DATE(LEFT(A3;4);MID(A3;5;2);RIGHT(A3;2))
but the locale might have been a factor and when dealing with date serial numbers and/or what appears to be a date but is a string various other issues might have been involved.
Consider:
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))