问题
I have a table to lookup a value like this:
logical test | points
-------------|-------
<= 0 | 1
<= 10 | 2
<= 20 | 4
> 20 | 5
If my cell is (A1 for example) <= 0 the result is 1...
if my cell is <=10 the result is 2 ...
if my cell is > 20 the result is 5.
I could use several if functions for that, but I want to prevent the user to have to change the formula if the table changes. For example, if, for some reason, the first line changes to > 0 it wouldn't be necessary to change the formula.
I have tried using concatenate(mycell;logical_operator;logical_value)
inside the if function but it doesn't seem to work, and I prefer a solution without VBA...
Any suggestions?
回答1:
I understand you want to be able to change the types of operators. I'm assuming the only operators you're working with are numerical comparisons: <, <=, =, >=, >
This is easier to set up with named ranges. Set the name for the ranges of Operators, Values and Scores as per my screenshot.
E.g. my range of Operators is "$C$2:$C$5", Values is "$D$2:$D$5" and Scores is "$E$2:$E$5"
Then, use this array formula to get the score for the value in A1.
=INDEX(Scores,MATCH(TRUE,IF(Operators="<",A1<Values,(IF(Operators="<=",A1<=Values,IF(Operators="=",A1=Values,IF(Operators=">=",A1>=Values,A1>Values))))),0))
I've put this formula into cell A3.
NOTE: This is an array formula. You need to enter it in a special way: Double click into a cell, paste the formula, then press CTRL-SHIFT-ENTER. You need to do this everytime you edit the formula, which should only be required if you need to edit the range of conditions. If this is done correctly, when you select the cell, the formula bar will show the formula with {curly braces} automatically placed around the formula.
The formula checks which operator you have in column C, then performs the appropriate comparison against column D. It performs this for every condition and finds the first condition that is TRUE. It then returns the score for that condition.
If there is more than one matching condition, then it returns the score for only the first matching condition. If there are no matching conditions, then it will return #N/A. (Or, if you have any blank rows in your range of conditions, it will return 0.)
回答2:
I may be misunderstanding your question, so let me know if this is not right, but this looks like exactly the right use case for non exact look-ups/matches & a lookup table.
Suppose you have a table. Then you can do a vlookup on your value (a1 in your example) to the lookup table, with the last parameter as a 1 instead of a 0 (which tells Excel to look for the value less than or equal to your provided value, instead of the exact provided value.
This meant that, in your given example of changing <=0 to >0, you would have to know the end point that you wanted on your 0 range. If you wanted anything greater than 0 to return a value, then you would have to remove all subsequent values.
This essentially gives you the power to leave the formula intact and never have to change it, while you only change the lookup table.
Here's an example to visualize what I'm talking about:
As you outlined in your original post, this will actually work if the underlying values change, BUT it has to be change of a certain type. Your table has to be sorted, and the intervals have to be fairly well defined, in the format [x1, x2], [x3, x4] etc. As long as you have control over the formatting of your table, you should be able to enforce this.
回答3:
if your input value is in the cell A1
then type the following formulat into cell B1
:
=IF(A1<=0,1,IF(A1<=10,2,IF(A1<=20,4,IF(A1>20,5))))
in my example I filled down values 0 through 27 in column A
cells A1
through A28
and copied this formula from B1
to B28
with following test results:
来源:https://stackoverflow.com/questions/42034984/excel-using-cell-reference-as-a-logical-operator-and-looking-up-a-value