I've found similar questions, but they're all at least slightly different from my question, and have been unable to successfully adapt them, so here's a simplified version of my sheet:
A B C D E
1 4 4 17
2 6 10
3 2 12
4 7 19
5 4 23
Column A is full of the integers 1-X. Column B is number of occurrences of Column A, and C is the sum of the values to the left and above. D is a random value between 1 and C5. So far so good. The problem comes with E1. I want it to give the value of A that is to the left of the C value that is the closest to D without going over.
Example: D comes up with the value 17. The closest value to 17 without going over is 12 (C3). Therefore, E equals 3. How would I go about achieving this? I can get the closest value using =INDEX(A$1:A$5,MATCH(MIN(ABS(F1-C$1:C$5)),ABS(F1-C$1:C$5),0)), but it comes to 4, instead of 3. How would I get the closest SMALL value? I'm guessing I have to replace ABS with SMALL, but I'm not sure how to go about doing that.
You can use the "vector form" of LOOKUP for this (see help for LOOKUP function). If you lookup D1 in C1:C5 you'll get exactly the match you want (the largest value that's smaller than or equal to D1) and then you can define the return vector as A1:A5 to get the corresponding value from there
=LOOKUP(D1,C$1:C$5,A$1:A$5)
Note: You'll get an error for D1 values < 4 because in that case there's no value "without going over".
For this to work C1:C5 must be sorted ascending, but that will always be the case in this scenario
Edit: I believe this answers your question, as stated, but if you were looking for the 17th occurrence, in order then shouldn't the result be 4? If that was the case then I think you could still use LOOKUP but column C would have to be set up differently
In E1: =if(C1<=D$1,A1,"") and fill down.
In F1: =max(E1:E5). This is your desired result.
Related
I have a table, the left hand column consisting of questionnaire participants say 1-20. Followed by 10 columns with answers to 10 possible questions.
Lets say question answers range from a-e where a = 1 point and e = 5 points.
What is the easiest and quickest way to sum up everyones score.
I have attempted to use Sumifs, giving a score to each letter (a=1,b=2...), however, can't figure out how I would formulate the sumif.
I have considered VBA to score each question one by one and them sum the total - however I feel this is very long winded.
Example table (You may use '=CHAR(RANDBETWEEN(97,101))' in order to randomise the letters for each question)
Questionnaire / Question 1 / Question 2/ ... / Question 10
1 a c d
2 d e b
... ... ... ...
20
You can use:
=SUMPRODUCT(CODE(UPPER(B2:K21))-64)
In VBA:
Total = Evaluate("=SUM(CODE(UPPER(B2:K21))-64)")
This is an array formula, and that's why SUMPRODUCT() is better on a sheet, but VBA can work with array without entering as array, hence why the SUM() works through VBA. It's sheet equivalent would be: {=SUM(CODE(UPPER(B2:K21))-64)}
Use this formula on all responses adjusting A1 to the cell needed.
=CODE(UPPER(A1))-64
This converts a or A to 1, b or B to 2 etc. This works because the ASCII value of upper case A is 65. Then you can add the values.
I'm trying to use an AND operator inside MAX and IF functions and am having trouble. I want to find the maximum value from column C where both are true:
column A = $D$1
the max only considers the top 80% of the values in column B
So in the example below, the answer would be 7, since of the rows with column A = "foo" and the top 80% of column B which are 3,4,5,6 the max value is 7.
I have two components working:
=MAX(IF($A$1:$A$12=$D$1,$C$1:$C$12))
returns 12
=MAX(IF(B1:B12>PERCENTILE(B1:B12,0.2),C1:C12))
returns 8
If I put them together like this it returns 0 (even with ctrl+shift+enter):
=MAX(IF(AND($A$1:$A$12=$D$1,B1:B12>PERCENTILE(B1:B12,0.2)),C1:C12))
The solution with both AND clauses should be 7
Can anyone help with this?
please modify array formula to:
=MAX(IF(A1:A12=$D$1,IF(B1:B12>PERCENTILE(B1:B12,0.2),C1:C12)))
end with ctrl+shift+enter
Multiplying the booleans together did the trick:
=MAX(IF(($A$1:$A$12=$D$1)*(B1:B12>PERCENTILE(B1:B12,0.2)),C1:C12))
I don't know why multiplying works but AND doesn't. God I hate Excel.
I have googled for hours, not being able to find a solution to what I need/want. I have an Excel sheet where I want to sum the values in one column based on the criteria that either one of two columns should have a specific value in it. For instance
A B C
1 4 20 7
2 5 100 3
3 100 21 4
4 15 21 4
5 21 24 8
I want to sum the values in C given that at least one of A and B contains a value of less than or equal to 20. Let us assume that A1:A5 is named A, B1:B5 is named B, and C1:C5 is named C (for simplicity). I have tried:
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
which gives me the rows where both columns match summed twice, and
={SUMPRODUCT(C,((A<=20)*(C<=20)))}
which gives me only the rows where both columns match
So far, I have settled for the solution of adding a column D with the lowest value of A and B, but it bugs me so much that I can't do it with formulas.
Any help would be highly appreciated, so thanks in advance. All I have found when googling is the "multiple criteria for same column" problem.
Thanks. That works. Found another one that works, after I figured out that excel does not treat 1 + 1 = 1 as I learnt in discrete mathematics, but as you say, counts the both the trues. Tried instead with:
{=SUM(IF((A<=20)+(B<=20);C;0))}
But I like yours better.
Your problem that it is "summing twice" in this formula
={SUMPRODUCT(C,((A<=20)+(C<=20)))}
is due to addition turning first TRUE plus the second TRUE into 2. It is not actually summing twice, because for any row, if only one condition is met, it would count that row only once.
The solution is to transform either the 1 or the 2 into a 1, using an IF:
={SUMPRODUCT(C,IF((A<=20)+(C<=20))>0, 1, 0)}
That way, each value in column C would only be counted at max once.
Following this site you could build up your SUMPRODUCT() formula like this:
=SUMPRODUCT(C,SIGN((A<=20)+(C<=20)))
So, instead of a nested IF() you control your or condition with the SIGN()function.
hth
If you plan to use a large set of data then it is best to use the array formula:
{=SUM(IF((A1:A5<=20)+(B1:B5<=20),C1:C5,0))}
Obviously adjust the range to suit the data set, however if the whole of each column is to form part of the formula then you can simply adjust to:
{=SUM(IF((A:A<=20)+(B:B<=20),C:C,0))}
This will perform the calculation on all rows of data within the A, B and C columns. With either example remember to press Ctrl + Shift + Enter in order to trigger the array formula (as opposed to typing the { and }).
I have this, for example:
ColA ColB
X 1
Y 2
Z 3
X 4
I want to be able to summarize all values in Column B which
Column A=X or
Column A=Y.
The result should be 7 (1+2+4).
I did this:
SUM(IF(COUNTIF(A:A,"X"),VLOOOKUP("X",A:B,2,),"0"), IF(COUNTIF(A:A,"Y"),VLOOOKUP("Y",A:B,2,),"0"))
For some reason, it returns 3. It doesn't adds the second value of X for some reason.
Any ideas why?
Thanks!
=SUMPRODUCT(((A2:A5="X")+(A2:A5="Y"))*(B2:B5))
If you select a portion of the formula and press Ctrl+=, you can see how it is evaluated.
=SUMPRODUCT((({TRUE;FALSE;FALSE;TRUE})+({FALSE;TRUE;FALSE;FALSE}))*(B2:B5))
Now when those two arrays are added together, the TRUE is coerced to a 1 and the FALSE to a zero.
=SUMPRODUCT(({1;1;0;1})*(B2:B5))
The resulting array of 1's and 0's is multiplied by the array from B2:B5.
=SUMPRODUCT({1;2;0;4})
And summed up to 7.
Your formula returns an error (tooo many o’s!) but with VLOOKUPs 3. Since the problem is not with Y, simplify the issue by taking out that part of the formula:
=IF(COUNTIF(A:A,"X"),VLOOKUP("X",A:B,2,),"0")
This results in 1. But so does:
=VLOOKUP("X",A:B,2,)
Hence COUNTIF(A:A,"X") (which returns 2 because there are two instances of X) does not actually help. Replaced with 7, or 103 or 5=5 - no difference.
You are obviously aware that plain vanilla VLOOKUP stops ‘searching’ once it finds the first instance that meets its ‘rules’ but unfortunately inserting a 2 with COUNTIF is not enough to ‘tell’ VLOOKUP “after finding the first match, now go off and find the second as well”.
So an answer to your question as expressed is “Yes. VLOOKUP cannot be made aware of multiple instances with the =COUNTIF function.”
So I have a sheet similar to this:
A B C D E F
1 Name Age Number Gender Player player No.
2 Droid12 11 M Droid12 F3
3 R2D2 13 M C3P0 F12
4 C3P0 12 F Bot13 Y7
5 YVH7707 11 F J34 Z2
6 Bot13 15 M
7 Slim33 13 F
8 ABot43 14 F
9 DBo11 11 M
10 J34 12 M
I am trying to fill in Column C with the player number, if the person in question has one (Imagine that the sheet is thousands of time this large).
I have the following VLookup function in each cell in C (copied down from C1, of course):
=VLOOKUP(A2, $E$2:$F$5, 2,FALSE)
And am getting the result:
#N/A
When I try to step through, I get the error
Sheet1!$A$2 = Droid12: The cell currently being evaluated contains a
constant
Anyone have an idea what I'm doing wrong?
Thank you!
EDIT
I've tried some of these fixes, with no positive results. I tried this in C1-C3:
C2 contains:
=IF(A2=E2, F2, FALSE)
With the result that cell C2 contains the value Droid12
C3 contains:
=VLOOKUP(A2, $E$2:$F$3, 2, FALSE)
and is getting a #N/A error (with the same error:
Sheet1!$A$2 = Droid12: The cell currently being evaluated contains a
constant
The values are all standardized (Trimmed,etc...), and there is definitely a match in the range I'm comparing to, so I really can't see what I'm doing wrong. Anyone ever experience this before?
edit 2
I fixed it, turns out I had Player No. In column E and Player in Column F, so the comparison for some reason was not running correctly. I switched those two entities around, and the VLOOKUP worked fine. Weird, but I'm not complaining. Thanks to everyone who tried to help!
Usually it happens if there is no exact match. Try to use trim and wildcard chars to allow matching to skip spaces. For example:
=VLOOKUP(CONCATENATE("*",TRIM(A2),"*"), $E$2:$F$5, 2,FALSE)
Use this formula:-
=IF(ISERROR(VLOOKUP(A2,E:F,2,FALSE)), "",VLOOKUP(A2,E:F,2,FALSE))
In Simple, formula is
=VLOOKUP(A2,E:F,2,FALSE)
Issue is caused due to the Range, which is mentioned as $E$2:$F$5.
When you are trying to drag the formula for the rest of cells, Range is getting updated wrongly. This is causing issue.
The Cell which doesn't match the actual value then VLOOKUP returns #N/A. To overcome this, I have placed a IF condition to check any error and display empty if fails otherwise the value.
Sometimes the constant error can be avoided if you have the reference data on the furtherest left-hand column on all sheets.
i.e.
Column A1 = Description plus info A2:etc (Sheet 1 and Sheet 2)
The N/A is the result of the name you are looking up not being in the list. This is normal behavior for the VLOOKUP function.
To eliminate the NA being displayed try this formula:
=IF(ISNA(VLOOKUP(A2,$E$2:$F$5, 2, FALSE)),"",VLOOKUP(A2,$E$2:$F$5, 2, FALSE))
This checks to see if the result of the lookup is N/A. If it is, then display blank (""). If the result is not NA then display the lookup value.
One issue with this solution is the lookup will be performed twice on each record that is found (once to check if it is N/A and once again to display the value (although Excel may be optimizing for this situation).