If statement depending on time of day - excel

I need to use a specific time, so far I had this
=IF(AND(TIME(15,45,0)<=(AW15=$A$11,$A$13)),IF(AND(TIME(15,45,0)>=(AW16=$A$13,$A$15))
So if before 15:45:00 I need for it to equal this logical test AW15=$A$11,$A$13 and if after 15:45:00 to equal to AW16=$A$13,$A$15.
Hope it makes sense and thanks in advance.

So it sounds like you are trying to write a nested IF statement. That is, IF A is true, THEN IF B is also true THEN return result 1, otherwise (A true, but B not true) return result 2, otherwise (A not true, B not tested) return result 3.
In Excel, this would be written as follows:
=IF(Parameter1=Condition1,IF(Parameter2=Condition2,Result1,Result2),Result3)
Applying it to your scenario, I think you are aiming for this:
=IF(TIME(HOUR(NOW()), MINUTE(NOW()), SECOND(NOW())) < TIME(15,45,0),
IF(AW15=$A$11,$A$13,"Condition1.2"),
IF(AW16=$A$13,$A$15,"Condition2.2"))
Note, some scenarios have not been covered by your statement, so I have written "Condition1.2" and "Condition2.2" which you can replace with additional tests or results to return.
Condition1.2 is where the time is before 15:45, but AW15 did NOT equal A11.
Condition2.2 is where the time is at or after 15:45, but AW16 did NOT equal A13.
You don't have to put anything in those placeholders if you don't want to, but if either of those conditions are ever met then the formula will simply return "FALSE".
Also, if you do not want the test time to be NOW(), then you will need to reference another cell that contains a fixed timestamp for when the row is being worked. NOW() is volatile, which means if you save the spreadsheet before 15:45 but then open it again after 15:45, the results you had already calculated will all change.

If i understand your question correctly, you need to compare the time now to 15:45:00 and make a selection based on that. If so the solution is:
=IF(TIME(HOUR(NOW()), MINUTE(NOW()), SECOND(NOW())) < TIME(15,45,0),
AW15=$A$11,$A$13, AW16=$A$13,$A$15)
This translates to: If the time now is before 15:45:00 then do AW16=$A$13,$A$15 else do AW16=$A$13,$A$15
If you want to compare the time in a specific cell then substitute the TIME(HOUR(NOW()), MINUTE(NOW()), SECOND(NOW())) with a cell which has a time for example:
=IF(A1 < TIME(15,45,0),
AW15=$A$11,$A$13, AW16=$A$13,$A$15)

Related

Excel Statement with 4 conditions and 4 answers

All of the methods that I've used have 2 answer values (True or False).
How can I get the following with a formula?
If A1=1 then it's 11, if A1=2 the answer is 22, if A1=3 then it's 33, if A1=4 it's 44.
If the value your are evaluating is in cell A1, then the nested function would be as follows:
IF(A1=1,11,IF(A1=2,22,IF(A1=3,33,IF(A1=4,44,""))))
I put the 2 double commas at the end so the formula returns a blank instead of false.
I don't know if that's what you are asking about, but you can make multiple (nested) IF statements in one. For example:
IF(1=2;TRUE;IF(2=2;TRUE;FALSE))
You just put another IF in the FALSE part of IF statement. If that's not it, can you give a piece of the statement you tried and precise more what do you want?
=IF(AND(INT(A1)=A1,A1<=4,A1>=1),A1*11,"")
Now the above works for the condition you placed in your example, however if one were to go by your title alone you have a couple of options you could go with.
You first Option would be nested IF statements. Like you said each IF function has TRUE or FALSE. The trick is to put another IF function in for the TRUE result and another in for the FALSE results
IF(CHECK1, IF(CHECK2, TRUE2, FALSE2),IF(CHECK3, TRUE3, FALSE3))
The above give 4 potential results based on only 3 checks. Another option would be to do a check and supply a value for a TRUE result and another IF for a false result. Keep repeating the process. Conversely you could go the same route flipping TRUE FALSE option. It might look something like this:
IF(CHECK1, TRUE1, IF(CHECK2, TRUE2, IF(CHECK3, TRUE3, FALSE3)))
FALSE3 would be the result of all previous checks failing.
So for your case, your nested IF could look like (assuming the only valid entries are 1, 2, 3 and 4):
IF(A1=1,11,IF(A1,2,22,IF(A1=3,33,44)))
OR
IF(ISODD(A1),IF(A1=1,11,33),IF(A1=2,22,44))
and there are other options to work through the logic. there are also other checks you could be doing and results being displayed if your entries in A1 were not limited to the integers 1,2,3 and 4.
Now because you example is using the sequential integers 1,2,3 and 4 you could also use the CHOOSE function. Alternatively if you can make your criteria evaluate to sequential integers stating at 1 the CHOOSE function would work as well. Supply choose with an integer as the first argument and it will return the corresponding argument in the list that follows
CHOOSE(ARGUMENT,RESULT1, RESULT2,...,RESULTn-1, RESULTn)
In your case it would look something like:
CHOOSE(A1,11,22,33,44)
If you can not get sequential numbers for whatever reason and the gap is numbers is small and you are in the low integer count, you could leave a gap in results by providing "", or 0). lets say you has 1,3 and 4 as potential arguments, then your choose might look like:
CHOOSE(A1,11,"",33,44)
=IF(A1<>"",INDEX({11;22;33;44},A1),"")
=IF(AND(ISNUMBER(A1),A1<=4),A1*11,"")

Conditional IF() statement problem, not returning desired value

Currently writing a program in excel that will return a value based on user input. The current formula has 5 different return options which are returned based on the selection of a number by the user. I use the IF() statement embedded into more IF() statements to account for multiple input options. However, when I go to enter in a number beyond the range of the first IF() statement, I am getting 0 even though it should be a different number.
For the code below, C30 is the input cell and it should return .15 if I was to enter 25.
=IF(C30<20, 0.35, IF(20<C30<40, 0.15, IF(40<C30<60, 0, IF(60<C30<80, -0.1, IF(80<C30, -0.2, 0)))))
From the logic statements, it should be returning .15, but all I am getting is 0.
Excel does not use 20<C30<40 it would be:
AND(20<C30,C30<40)
But you can shorten this with a simple MATCH and CHOOSE:
=CHOOSE(MATCH(C30,{0,20,40,60,80}),0.35,0.15,0,-0.1,-0.2)
If you really want a nested if there is no need for the extra tests:
=IF(C30<20,0.35,IF(C30<40,0.15,IF(C30<60,0,IF(C30<80,-0.1,-0.2))))
IF will resolve sequentially and short circuit as soon as it finds the first TRUE, so it does not need the other logic.
The problem here is the logic that you have used to evaluate whether C30 falls within a range of numbers.
IF(20<C30<40,...) will not check whether C30 is in the range of 20 through 40.
Instead, use AND(cond1, cond2, ...) to check whether the values are within the range:
IF(AND(C30 > 20, C30 < 40), ...)
Replace terms like:
20<C30<40
with:
AND(20<C30,C30<40)
etc.

Use "if" function to identify specific hours

I have an excel table see attached picture. I want to find which cells (rows) contain specific hours. To do so I used the if function:
=IF(B2=HOUR("12:00:00");"xx";"yes")
The outcome was not correct. As you can see the outcome was "yes" corresponding to false.. What is the problem ?
In addition, I tried to embed the "and" function, in order to select multiple hours, but the excel prompt error message. The formula I tried in cell B2 is:
=IF(B2=HOUR(AND("12:00:00";"3:00:00");"xx";"yes"))
error message:
contain many conditions
I think you have swapped the arguments in your comparison.
According to the documentation
[HOUR] returns the hour of a time value
If we evaluate your formula step by step, we get:
HOUR("12:00:00") -> 12
B2 = 12 -> false
IF(false; "xx"; "yes") -> yes
To fix this, change your condition to HOUR(B2) = 12.
That should work for a single hour.
If you want to check for multiple hour values, you can use the OR function, as noted in a previous answer, but with modified conditions like this:
IF(OR(HOUR(B2) = 3; HOUR(B2) = 12); "xx"; "yes")
First you just have misplaced the return of the true and the return of the false. Try :
=IF(B2=HOUR("12:00:00");"yes";"xx")
Second, you do not want a logical and but a logical or. And your condition is "if B2 is an hour that is equals to '12:00 and 3:00'" this has no sense. What you want is "if B2 is an hour that is equals to '12:00' or B2 is an hour that is equals to '3:00'". This should look like :
=IF(OR(B2=HOUR("12:00:00"), B2=HOUR("3:00:00")) ; "yes"; "xx"))
Edit : Thanks to Korrat answer I can correct mine. The hour should not be compared like this. You should use the function HOUR on the cell and not on the value.
=IF(OR(HOUR(B2)=12, HOUR(B2)=3) ; "yes"; "xx"))

combining IF and AND statement not working in excel

I am trying to calculate the percentage, here the rules are as follows:
01. Employees working in IT department for more than 10 years will get 7% and rest of the IT guys will get 6.5%
02. And for rest of the departments, we have different percentages
Here H column represents various departments and F is working experience and in column I we're getting the main value from which we have to calculate the percentages.
Here's what I tried
=IF(AND(H5="IT",F5<10),I5*6.5%,I5*7%,IF(H5="PRODUCTION",I5*9%,IF(H5="MARKETING",I5*6%,IF(H5="LAW",I5*6%,IF(H5="HR",I5*9.36%)))))
This is showing You've entered too many arguments
Your first If statement, really had one too many argument.
=IF(AND(H5="IT",F5<10),I5*6.5%,IF(H5="PRODUCTION",I5*9%,IF(H5="MARKETING",I5*6%,IF(H5="LAW",I5*6%,IF(H5="HR",I5*9.36%,I5*7%)))))
Since each new if is nested as the FALSE eventuality. Look at the end for that 7%.
Edit:
My bad(reading your comment made me realize), there are two error in your formula.
One has been discussed, the second is how you nested those IT percentage.
=IF(AND(H5="IT",F5>=10),I5*7%,IF(AND(H5="IT",F5<10),I5*6.5%,IF(H5="PRODUCTION",I5*9%,IF(H5="MARKETING",I5*6%,IF(H5="LAW",I5*6%,IF(H5="HR",I5*9.36%,""))))))
In this version I added another IF statement. We could have avoided it by defining either one of the IT's rate as the ELSE result same as my initial answer. Now all predictable eventuality have their own TRUE match. An unexpected value will return an empty string. Maybe you want it to be 0 instead...
(1) IF(AND(...))= conditions
(2) I5 * 6.5% = value if true
(3) I5 * 7% = value if false
Therefore, you already indicate value if false and another if statement is not allowed. I would remove (3) and put it to the very end of your formula. As such, once none of the previous conditions are met, it will automatically take on the value of 7%.

Case Function Equivalent in Excel

I have an interesting challenge - I need to run a check on the following data in Excel:
| A - B - C - D |
|------|------|------|------|
| 36 | 0 | 0 | x |
| 0 | 600 | 700 | x |
|___________________________|
You'll have to excuse my wonderfully bad ASCII art. So I need the D column (x) to run a check against the adjacent cells, then convert the values if necessary. Here's the criteria:
If column B is greater than 0, everything works great and I can get coffee. If it doesn't meet that requirement, then I need to convert A1 according to a table - for example, 32 = 1420 and place into D. Unfortunately, there is no relationship between A and what it needs to convert to, so creating a calculation is out of the question.
A case or switch statement would be perfect in this scenario, but I don't think it is a native function in Excel. I also think it would be kind of crazy to chain a bunch of =IF() statements together, which I did about four times before deciding it was a bad idea (story of my life).
Sounds like a job for VLOOKUP!
You can put your 32 -> 1420 type mappings in a couple of columns somewhere, then use the VLOOKUP function to perform the lookup.
Without reference to the original problem (which I suspect is long since solved), I very recently discovered a neat trick that makes the Choose function work exactly like a select case statement without any need to modify data. There's only one catch: only one of your choose conditions can be true at any one time.
The syntax is as follows:
CHOOSE(
(1 * (CONDITION_1)) + (2 * (CONDITION_2)) + ... + (N * (CONDITION_N)),
RESULT_1, RESULT_2, ... , RESULT_N
)
On the assumption that only one of the conditions 1 to N will be true, everything else is 0, meaning the numeric value will correspond to the appropriate result.
If you are not 100% certain that all conditions are mutually exclusive, you might prefer something like:
CHOOSE(
(1 * TEST1) + (2 * TEST2) + (4 * TEST3) + (8 * TEST4) ... (2^N * TESTN)
OUT1, OUT2, , OUT3, , , , OUT4 , , <LOTS OF COMMAS> , OUT5
)
That said, if Excel has an upper limit on the number of arguments a function can take, you'd hit it pretty quickly.
Honestly, can't believe it's taken me years to work it out, but I haven't seen it before, so figured I'd leave it here to help others.
EDIT: Per comment below from #aTrusty:
Silly numbers of commas can be eliminated (and as a result, the choose statement would work for up to 254 cases) by using a formula of the following form:
CHOOSE(
1 + LOG(1 + (2*TEST1) + (4*TEST2) + (8*TEST3) + (16*TEST4),2),
OTHERWISE, RESULT1, RESULT2, RESULT3, RESULT4
)
Note the second argument to the LOG clause, which puts it in base 2 and makes the whole thing work.
Edit: Per David's answer, there's now an actual switch statement if you're lucky enough to be working on office 2016. Aside from difficulty in reading, this also means you get the efficiency of switch, not just the behaviour!
The Switch function is now available, in Excel 2016 / Office 365
SWITCH(expression, value1, result1, [default or value2, result2],…[default or value3, result3])
example:
=SWITCH(A1,0,"FALSE",-1,"TRUE","Maybe")
Microsoft -Office Support
Note: MS has updated that page to only document the behavior of Excel 2019. Eventually, they will probably remove references to 2019 as well... To see what the page looked like in 2016, use the wayback machine:
https://web.archive.org/web/20161010180642/https://support.office.com/en-us/article/SWITCH-function-47ab33c0-28ce-4530-8a45-d532ec4aa25e
Try this;
=IF(B1>=0, B1, OFFSET($X$1, MATCH(B1, $X:$X, Z) - 1, Y)
WHERE
X = The columns you are indexing into
Y = The number of columns to the left (-Y) or right (Y) of the indexed column to get the value you are looking for
Z = 0 if exact-match (if you want to handle errors)
I used this solution to convert single letter color codes into their descriptions:
=CHOOSE(FIND(H5,"GYR"),"Good","OK","Bad")
You basically look up the element you're trying to decode in the array, then use CHOOSE() to pick the associated item. It's a little more compact than building a table for VLOOKUP().
I know it a little late to answer but I think this short video will help you a lot.
http://www.xlninja.com/2012/07/25/excel-choose-function-explained/
Essentially it is using the choose function. He explains it very well in the video so I'll let do it instead of typing 20 pages.
Another video of his explains how to use data validation to populate a drop down which you can select from a limited range.
http://www.xlninja.com/2012/08/13/excel-data-validation-using-dependent-lists/
You could combine the two and use the value in the drop down as your index to the choose function. While he did not show how to combine them, I'm sure you could figure it out as his videos are good. If you have trouble, let me know and I'll update my answer to show you.
I understand that this is a response to an old post-
I like the If() function combined with Index()/Match():
=IF(B2>0,"x",INDEX($H$2:$I$9,MATCH(A2,$H$2:$H$9,0),2))
The if function compare what is in column b and if it is greater than 0, it returns x, if not it uses the array (table of information) identified by the Index() function and selected by Match() to return the value that a corresponds to.
The Index array has the absolute location set $H$2:$I$9 (the dollar signs) so that the place it points to will not change as the formula is copied. The row with the value that you want returned is identified by the Match() function. Match() has the added value of not needing a sorted list to look through that Vlookup() requires. Match() can find the value with a value: 1 less than, 0 exact, -1 greater than. I put a zero in after the absolute Match() array $H$2:$H$9 to find the exact match. For the column that value of the Index() array that one would like returned is entered. I entered a 2 because in my array the return value was in the second column. Below my index array looked like this:
32 1420
36 1650
40 1790
44 1860
55 2010
The value in your 'a' column to search for in the list is in the first column in my example and the corresponding value that is to be return is to the right. The look up/reference table can be on any tab in the work book - or even in another file. -Book2 is the file name, and Sheet2 is the 'other tab' name.
=IF(B2>0,"x",INDEX([Book2]Sheet2!$A$1:$B$8,MATCH(A2,[Book2]Sheet2!$A$1:$A$8,0),2))
If you do not want x return when the value of b is greater than zero delete the x for a 'blank'/null equivalent or maybe put a 0 - not sure what you would want there.
Below is beginning of the function with the x deleted.
=IF(B2>0,"",INDEX...
If you don't have a SWITCH statement in your Excel version (pre-Excel-2016), here's a VBA implementation for it:
Public Function SWITCH(ParamArray args() As Variant) As Variant
Dim i As Integer
Dim val As Variant
Dim tmp As Variant
If ((UBound(args) - LBound(args)) = 0) Or (((UBound(args) - LBound(args)) Mod 2 = 0)) Then
Error 450 'Invalid arguments
Else
val = args(LBound(args))
i = LBound(args) + 1
tmp = args(UBound(args))
While (i < UBound(args))
If val = args(i) Then
tmp = args(i + 1)
End If
i = i + 2
Wend
End If
SWITCH = tmp
End Function
It works exactly like expected, a drop-in replacement for example for Google Spreadsheet's SWITCH function.
Syntax:
=SWITCH(selector; [keyN; valueN;] ... defaultvalue)
where
selector is any expression that is compared to keys
key1, key2, ... are expressions that are compared to the selector
value1, value2, ... are values that are selected if the selector equals to the corresponding key (only)
defaultvalue is used if no key matches the selector
Examples:
=SWITCH("a";"?") returns "?"
=SWITCH("a";"a";"1";"?") returns "1"
=SWITCH("x";"a";"1";"?") returns "?"
=SWITCH("b";"a";"1";"b";TRUE;"?") returns TRUE
=SWITCH(7;7;1;7;2;0) returns 2
=SWITCH("a";"a";"1") returns #VALUE!
To use it, open your Excel, go to Develpment tools tab, click Visual Basic, rightclick on ThisWorkbook, choose Insert, then Module, finally copy the code into the editor. You have to save as a macro-friendly Excel workbook (xlsm).
Even if old, this seems to be a popular questions, so I'll post another solution, which I think is very elegant:
http://fiveminutelessons.com/learn-microsoft-excel/using-multiple-if-statements-excel
It's elegant because it uses just the IF function. Basically, it boils down to this:
if(condition, choose/use a value from the table, if(condition, choose/use another value from the table...
And so on
Works beautifully, even better than HLOOKUP or VLOOOKUP
but... Be warned - there is a limit to the number of nested if statements excel can handle.
Microsoft replace SWITCH, IFS and IFVALUES with CHOOSE only function.
=CHOOSE($L$1,"index_1","Index_2","Index_3")
Recently I unfortunately had to work with Excel 2010 again for a while and I missed the SWITCH function a lot. I came up with the following to try to minimize my pain:
=CHOOSE(SUM((A1={"a";"b";"c"})*ROW(INDIRECT(1&":"&3))),1,2,3)
CTRL+SHIFT+ENTER
where A1 is where your condition lies (it could be a formula, whatever). The good thing is that we just have to provide the condition once (just like SWITCH) and the cases (in this example: a,b,c) and results (in this example: 1,2,3) are ordered, which makes it easy to reason about.
Here is how it works:
Cond={"c1";"c2";...;"cn"} returns a N-vector of TRUE or FALSE (with behaves like 1s and 0s)
ROW(INDIRECT(1&":"&n)) returns a N-vector of ordered numbers: 1;2;3;...;n
The multiplication of both vectors will return lots of zeros and a number (position) where the condition was matched
SUM just transforms this vector with zeros and a position into just a single number, which CHOOSE then can use
If you want to add another condition, just remember to increment the last number inside INDIRECT
If you want an ELSE case, just wrap it inside an IFERROR formula
The formula will not behave properly if you provide the same condition more than once, but I guess nobody would want to do that anyway
If your using Office 2016 or later, or Office 365, there is a new function that acts similarly to a CASE function called IFS. Here's the description of the function from Microsoft's documentation:
The IFS function checks whether one or more conditions are met, and returns a value that corresponds to the first TRUE condition. IFS can take the place of multiple nested IF statements, and is much easier to read with multiple conditions.
An example of usage follows:
=IFS(A2>89,"A",A2>79,"B",A2>69,"C",A2>59,"D",TRUE,"F")
You can even specify a default result:
To specify a default result, enter TRUE for your final logical_test argument. If none of the other conditions are met, the corresponding value will be returned.
The default result feature is included in the example shown above.
You can read more about it on Microsoft's Support Documentation

Resources