I am trying to write a formula and getting error. The logic is:
If A1 > 0 and B1 = "PEN" Output = "Refund 1"
If A1 > 0 and B1 = "INT" Output = "Refund 1"
If A1 > 0 and B1 = "ADM" Output = "Refund 1"
If A1 > 0 and B1 = "AB" Output = "Refund 2"
If A1 < 0 and B1 = "PEN" Output = "Fund 1"
If A1 < 0 and B1 = "INT" Output = "Fund 1"
If A1 < 0 and B1 = "ADM" Output = "Fund 1"
If A1 < 0 and B1 = "AB" Output = "Fund 2"
I am getting errors writing multiples If-statements.
Can someone help?
This will work for you.
=IF(AND(A1>0,OR(B1="PEN",B1="INT",B1="ADM")),"Refund 1",IF(AND(A1>0,B1="AB"),"Refund 2",IF(AND(A1<0,OR(B1="PEN",B1="INT",B1="ADM")),"Fund 1",IF(AND(A1<0,B1="AB"),"Fund 2"))))
While #Jeanno's answer works for this case, I wanted to point out that you can accomplish the same thing more generally using a lookup table.
Items in blue are hard-coded, black are formulas.
There are a couple advantages to doing it this way:
Extensible: if your criteria suddenly grew from testing two conditions to 3 or more, the IF style statement is going to go from almost illegible to completely illegible.
Clear: By looking up the value on a table, anyone can tell why a row is getting assigned to the result. Trying to do that with Excel's nested AND, OR, IF statements is really hard.
Related
I have two columns in a table: column_A with 3 different values a1, a2, a3 and column_B with 5 different values b1, b2, b3, b4, b5. I have to create column_C in Excel based on the following logic -
> if (A=a1)
> if(B=b1 or B=b2)
> output=a1_yes
> else
> output = a1_no
> elseif(A=a2)
> if(B=b3 or B=b4)
> output = a2_yes
> else
> output = a2_no
> elseif(A=a3)
> if(B=b5)
> output = a3_yes
> else
> output = a3_no
Nested if-else makes the logic very complex. Is there any function we could use for simplicity and avoid nested if-else?
Assuming you want to get rid of the nested if-else, I would suggest you use the function Switch instead.
You can concatenate the columns you are validating and provide results based on the possible values.
Given the table below:
Make 1
Model 1
Color 1
Use the formula:
SWITCH(expression,value1,result1,default_or_value2,result2,...)
Example:
=SWITCH(CONCAT(A1,B1),"Make1","a1_yes","Make2","a1_yes","a2_yes")
Reference
SWITCH Formula
I think in such complex cases it is best to create a lookup table and find the required value with the LOOKUP function which works starting from Excel 2007:
=LOOKUP(2,1/((A2=$H$2:$H$9)*(B2=$I$2:$I$9)+(A2=$H$2:$H$9)*(""=$I$2:$I$9)),$J$2:$J$9)
In the case of GS, this formula must be included in the ARRAYFORMULA function.
=ArrayFormula(LOOKUP(2,1/((A2=$H$2:$H$9)*(B2=$I$2:$I$9)+(A2=$H$2:$H$9)*(""=$I$2:$I$9)),$J$2:$J$9))
If you can't create lookup table here will be option without it:
=LOOKUP(2,
1/((A2={"a1","a1","a1","a2","a2","a2","a3","a3"})
*(B2={"","b1","b2","","b3","b4","","b5"})
+(A2={"a1","a1","a1","a2","a2","a2","a3","a3"})
*(""={"","b1","b2","","b3","b4","","b5"})),
{"a1_no","a1_yes","a1_yes","a2_no","a2_yes","a2_yes","a3_no","a3_yes"})
Hey there smart people,
I hope the title of my question is fitting enough, I tried my best ^^
I try to comepare 2 rows from Column A to Z and check if they have the same pattern inside. With pattern I mean that if in row 1 column A anything is written then there must also be something written in row 2 column A. Alternatively both can be empty.
Sounds simple but The if Statement is only allowed to be executed if the pattern DOESNT match so: something written in A1 and nothing written in A2.
I am sure that I use just a to complicated method but even after googling the issue I cant find a fitting, simple solution.
I am sure its a piece of cake for you guys but I just cant figure out how to structure it. (maybe a friday specific issue ;))
(In the code I skipped the loop part since I guess its unnecessary for the problem)
If Not (A1 = "" And A2 = "") Or Not (Not A1 = "" And Not A2 = "") Then
Boolean = False
End If
Symplifying your boolean expression shows it always avaluate to true, so there is a problem here.
Quick boolean algebra lesson:
NOT(A AND B) = NOT(A) OR NOT(B)
NOT(A OR B) = NOT(A) AND NOT(B)
When applied to your expression, writing for simplicity A1 = "" as A and A2 = "" as B:
Not (A And B) Or Not (Not A And Not B)
= Not(A) Or Not(B) or Not(Not(A)) or Not(Not(B))
= Not(A) Or Not(B) or A or B
= True
What you mean is, I believe, something like:
Not((A and B) or (Not(A) and Not(B)))
Which may be simplified, but this impairs readability (what you want to check becomes less obvious).
Finally, you shouldn't write something like this:
If Expression
Boolean = False
but rather:
Boolean = Not(Expression)
Assuming the variable assignment is not a placeholder for actual code.
Dim i As Long, result As Boolean
For i = 1 To 25
If Cells(1, i).Value = Cells(2, i).Value Then
result = True
Else
result = False
Exit For
End If
Next i
You can modify this code for your purpose, but whenever a column has a different value in both first and second row, result will be false, and true if they are equal.
Columns can be treated both as letters or numbers.
I'm having a hard time trying to figure what you want to do with that line of code, but here is my shot at it.
It will return Boolean = False only if A1 is empty and A2 isn't, or if A1 isn't empty but A2 is.
If (A1 = "" and A2 <> "") OR (A1 <> "" and A2 = "") Then
Boolean = False
End If
You can use a formula like the below to evaluate if every cell in a range is blank.
=SUMPRODUCT(--(range<>""))=0
and a similar formula to check if every cell is not blank:
=SUMPRODUCT(--(range=""))=0
and apply the formula using VBA, or else as a normal Excel formula.
Taken from this page:
https://exceljet.net/formula/all-cells-in-range-are-blank
Not an answer, but my conclusion is that your statement will always evaluate to TRUE
A slightly different approach
Sub x()
Dim b As Boolean
b=(WorksheetFunction.CountBlank(Range("A1:A2")) <> 1)
End Sub
Another one about what I understand :
if (A1<> "" and A2<>"") or (A1= "" and A2="") then
Boolean = False
end if
My formula gives #DIV/0 if range is empty/blank. Instead, I would like result to be "" or "0" instead.
=IFERROR("Total = $"&FIXED(SUBTOTAL(9,L3:L85),0,0)&"
≥ $10K Count = "&COUNTIF(L3:L85,">=10000"),"≥ $10K Count = 0")&" or "&FIXED(SUM(COUNTIF(L3:L502,">=10000")/COUNTA(L3:L502)*100),0)&"%"
Can anyone suggest how to improve the formula?
This section of your formula has the risk of an error of divide by zero:
COUNTIF(L3:L502,">=10000")/COUNTA(L3:L502)*100
So you just need to wrap that in another IFERROR function:
IFERROR(COUNTIF(L3:L502,">=10000")/COUNTA(L3:L502)*100,0)
And insert that back into the overall formula:
=IFERROR("Total = $"&FIXED(SUBTOTAL(9,L3:L85),0,0)&"
≥ $10K Count = "&COUNTIF(L3:L85,">=10000"),"≥ $10K Count = 0")&" or "&FIXED(SUM(IFERROR(COUNTIF(L3:L502,">=10000")/COUNTA(L3:L502)*100,0)),0)&"%"
I am having an issue with an excel problem and cannot use vba or add any extra columns. The problem goes along with the format of this image. I could not find anything on google that helped me with this problem and im sorry if it has been asked before.
Example Image
On a separate page in a cell i need to write a function that will check if Info 2 = "z" and Info4 = "x" and if that is true then i need to do the following equation with the numbers in Info1 and Info3: Info1*(1 - Info3)
I will also have to keep a sum of these numbers.
For this example I would want the cell with the formula to equal -34 by doing the following:
3*(1-4)+5*(1-6) = -34
I would want the cell to just display the finished sum
Any help would be greatly appreciated,
Thank you!
You are looking for the mighty powers of SUMPRODUCT
=SUMPRODUCT((B:B="z")*(D:D="x")*(A:A)*(1-C:C))
The first two multipliers will make sure we only evaluate those rows having z for B and x for D. While the latter two are your desired function. Excel will evaluate this for each row and sum up the results.
I am using psuedo values below but this should work:
= [value of cell above] + if(and([info2] = "z" , [info4] = "x"), [info1]*(1-[info3]),0)
so basically starting in the middle, you have a two truth tests,
[info2] = "z", [info4]= "x"
using AND() requires they both pass
and([info2] = "z", [info4]= "x")
if they do pass you want to do your formula:
if(and([info2] = "z" , [info4] = "x"), [info1]*(1-[info3]),FALSE)
but since we want to sum all values for each iterative row we make not passing this test 0:
if(and([info2] = "z" , [info4] = "x"), [info1]*(1-[info3]),0)
Ok so this works for one row, but doesn't sum the numbers from the tests on the previous row:
= [value of cell above or 0 for first row] + if(and([info2] = "z" , [info4] = "x"), [info1]*(1-[info3]),0)
an example written with real excel ranges that you may have to tweak depending on where your values are stored:
Sample picture
I am trying to write a statement that does the following:
If R7 = "Yes" AND S7 = "Yes" AND T7 = "Yes", then output 1
If R7 = "Yes" AND S7 = "Yes" AND T7 = "No", then output 2
If R7 = "Yes" AND S7 = "No" AND T7 = "Yes", then output 3
If R7 = "Yes" AND S7 = "No" AND T7 = "No", then output 4
IF anything else, then output 5
I have written the following statement:
=IF(R7="Yes",IF(S7="Yes",IF(T7="Yes",1),IF(R7="Yes",IF(S7="Yes",IF(T7="No",2),IF(R7="Yes",IF(S7="No",IF(T7="Yes",3),IF(R7="Yes",IF(S7="No",IF(T7="No",4)))))))),5)
I know I am close however, issue is when it should display 4, it just displays “FALSE” - have I got my brackets in the wrong order?
Any suggestions?
There's no need to nest so many IF statements, you can use the AND function.
=IF(AND(R7="Yes",S7="Yes",T7="Yes"),1,IF(AND(R7="Yes",S7="Yes",T7="No"),2,IF(AND(R7="Yes",S7="No",T7="Yes"),3,IF(AND(R7="Yes",S7="No",T7="No"),4,5))))
This should work for you.
For further reading, see the office documentation.
You need to construct your formula as follows ...
=IF(AND(R7="Yes",S7="Yes",T7="Yes"),1,(IF(AND(R7="Yes",S7="Yes",T7="No"),2,IF(AND(R7="Yes",S7="No",T7="Yes"),3,IF(AND(R7="Yes",S7="No",T7="No"),4,5)))))
Note that this function returns 5 when there is no matching combination ...