How to sum values from column B by choosing multiple different values from column A? - excel-formula

A 1
B 2
C 3
I want add number by choosing alphabet, and like if choose A & B in formula then cell must show sum value 3 and even if I shuffle the alphabet upside downside it still show the 3 by analyzing alphabet.

I first checked which of the key values are present in the user-provided input string. I made a new column where I assigned an extra value of 0 to those that are not present in the input string, and a value of 1 to those that are. I then multiplied that binary column with the corresponding value, and then summed them all up.
I started with FIND(), which will tell you where the value of the key letter starts in the input string. E.g., =FIND("A", "BAC") will return 2, because "A" starts in the second position of "BAC".
This will return an error if the string can't be found, so I surrounded the output of the FIND() with IFERROR(). IFERROR takes in an expression and lets you specify what you want the output to be if the expression returns an error. I set the error output value to be '0'.
I then surrounded all of that with an IF() statement. If the value is 0, leave it as 0. If it's greater than 0, set it to be 1.
This IF(IFERROR(FIND())) expression will produce a 0 or a 1 for each key letter, which you can then multiply with that key's value to obtain how much that key contributes to the final sum. And then it can all be added up neatly.
Screenshot of final spreadsheet
The important cell formulas are:
C1: (whatever your input string is)
C2: =SUM(C5:C7)
A5: A
A6: B
A7: C
B5: 1
B6: 2
B7: 3
C5: =IF(IFERROR(FIND(A5,$C$1),0)>0,1,0)*B5
C6: =IF(IFERROR(FIND(A6,$C$1),0)>0,1,0)*B6
C7: =IF(IFERROR(FIND(A7,$C$1),0)>0,1,0)*B7

Related

Logic behind last evaluation step of a LOOKUP formula to find last non empty value in a row.

I want to get last non empty cell from every row for further operations. Searching Net I came across a LOOKUP formula which can do this job fine. I have alpha as well as numeric data in the rows.
Sample Data is shown below.
ID1 ID2 ID3 ID4 ID5 ID6 ID7 ID8 ID9 ID10 ID11
a1 a2 a3 a4 a5 23 24 25 25
23 NN 67 99 200001 ss p1 23 rq 3
I am using the following formula in L4 which gives correct results that is 3 in L4. Similar formula for row 2 gives result of 25 in L2.
To understand the formula I carried out Formula Evaluation but I am not getting the logic of last step as how it translates to final result.
=LOOKUP(2,1/(A4:K4<>""),A4:K4)
LOOKUP(2,{1,1,1,1,1,1,1,1,1,#DIV/0!,1},A4:K4)
After this step result that is the last non empty value of Row 4 comes i.e. 3
Can someone explain how this line is finally translating into the result step by step.
Basically taking the below formula as an example
=LOOKUP(2,1/(A1:D1<>""),A1:D1)
With data in A1:D1
A1 B1 C1 D1
1 2 3
The formula will populate as follows
=LOOKUP(2,{1,1,1,#DIV/0!},{1,2,3,0})
The way this works is quite simple.
1/(A1:D1<>"") creates an array of 1's and #DIV\0!
1 where the cell is not blank
#DIV/0 where the cell is blank
Then the lookup formula looks for a 2 in this array , which it cant find, but it will always return the position of then last NON ERROR value in the context of an array filled with 1's and #DIV/0!, which would be the 3rd position in the array, The LOOKUP formula then returns the 3rd position of the last argument array in the formula A1:D1 , which is 3
This approach finding the last filled cell uses the vector form of LOOKUP. In
=LOOKUP(2,{1,1,1,1,1,1,1,1,1,#DIV/0!,1},A4:K4) the 2 is the lookup_value, the array {1,1,1,1,1,1,1,1,1,#DIV/0!,1} is the lookup_vector and the A4:K4 is the result_vector.
The vector form of LOOKUP looks in a lookup_vector for the lookup_value and returns the value from the same position in the result_vector.
If the LOOKUP function can't find the lookup_value directly in the lookup_vector, the function matches the largest value in lookup_vector that is less than or equal to lookup_value. If multiple equal values are in the array, which are the largest which are less than or equal to lookup_value, the position of the last is taken. In this case it matches the last 1 since this is the largest value in lookup_vector that is less than or equal to 2.
But for this to do, there is the rule, that the values in lookup_vector must be placed in ascending order. And this is the not documented feature with this approach. It seams as if the #DIV/0! errors within lookup_vector does not contradict that rule. But since the source is not open, we can't be absolutely sure about this. But this approach is used sucessfully so often, that we can be very sure.

Formula returning Column A value for row containing MAX value of a range

Assume I have the following table:
A B C
1 Week 1 Week 2
2 Melissa 114.7 82.8
3 Mike 105.5 122.5
4 Andrew 102.3 87.5
5 Rich 105.3 65.2
The names are in column A, the Week values are in Row 1. (So A1 is blank, B1 = Week 1, and A2 = Melissa.)
I'm trying to build a formula that looks at all the values in a known range (in this example, B2:C5), chooses the highest value of the bunch (here, 122.5) and returns the name of the person from Column A that got that value. If I use this formula, it works for the values in range B2:B5:
=INDEX(A2:A5,MATCH(MAX(B2:B5),B2:B5,0))
That returns Melissa but if I expand the range to include more than just column B's values, I get an #N/A returned:
=INDEX(A2:A5,MATCH(MAX(B2:C5),B2:C5,0))
The weird part (to my simple brain) is that the MATCH portion of the formula works fine, if I just put in this formula, it returns the highest value of 122.5 from C3:
=MAX(B2:C5,B2:C5,0)
So clearly something it going wrong when I'm using either the MATCH or INDEX commands.
Hopefully this makes sense and someone can point out my error?
Try this:
=INDEX(A:A,MAX((B2:C5=MAX(B2:C5))*ROW(B2:C5)))
This is an array formula and must be confirmed with Ctrl+Shift+Enter.
Note: Match can only search one vector at a time. It can be one row or one column or one array. It cannot be two or more rows or columns or a 2D array.
Do it "twice"? Please try:
=INDEX(A2:A5,IFERROR(MATCH(MAX(B2:C5),B2:B5,0),MATCH(MAX(B2:C5),C2:C5,0)))
If you are going to have up to 52/53 weeks to cope with I'd suggest instead inserting a helper column with the MAX for each row. Make that an new (inserted) ColumnA (say =MAX(C2:BC2) etc.) and a simple VLOOKUP should serve, say:
=VLOOKUP(MAX(A:A),A:B,2,0)

Alternating Columns on a Array Formula in Excel

I have a spreadsheet where data is spread in alternated columns. Columns A, C and E are flags indicating if the adjacent column has a valid data.
It is like this:
A B C D E F
1 1 32 0 67 1 34
The goal is to sum values where its left adjacent is 1. In this example, the sum should be 66, as A and E are both 1 and C is 0.
I can get an array with 1's and 0's indicating if a flag column is set or not:
=MOD(COLUMN(A1:F1),2)*A1:F1
And that gives me
{1, 0, 0, 0, 1, 0}
The thing is that I don't know what I can do from here. If I could slide all the data (by inserting a 0 at the beginning and removing the 0 at the last position), I could SUMPRODUCT it and get the result.
By the way, I can't use macros...
Ideas?
Notice that Formula bellow has two cell ranges one starts at column A the other at B.
=SUMIF(A1:F1,1,B1:G1)
If you are not famliar with SUMIF then what you need to know about this is that first term A1:F1 is where formula checks values for a condition. What condition you might ask values that equal to seconds term in this case =1. Lastly last term has the values that need to be sumed.
Also since you may have issuse of having 1 in an Even column( where you'd expect value not you condition), heve is a formula that makes sure that your 1 & 0 condition is in correct Column:
=SUM(IF((A1:F1=1)*ISODD(COLUMN(A1:F1)),B1:G1,0))
Simple version:
=SUM(A1*B1,C1*D1,E1*F1)
As 0 multiplied by anything is always zero then this only sums the columns preceded by a 1.

Using COUNTIFS for a series of values at once

Working a step higher then COUNTIFS, I appose a challenge to write a formula without VBA code. The basic data is combined from 1000s of rows with:
Column A: rows with values from 1 to 3
Column B: rows with values from 1 to 250.
For this purpose lets say, we are looking at all cells of value "1" in column A, that suit value "5" in column B. To find all matches, we'd use COUNTIFS command.
1 1
2 5
1 5
1 7
1 10
3 45
2 12
1 2
2 1
=COUNTIFS(A1:A9;1;B1:B9;5)
The answer here is 1.
Next thing, the "5" in column B belongs to a group, e.g. group from 1 to 9. What would the best way be, to count all the matches in this example, so that for all "1"'s in column A, we'd have to find all matches with values from 1 to 9 in column B?! In the upper example that would result in "4". The obvious solution is with a series of IF commands, but that's unefficient and it easy to make a mistake, that get's easily overseen.
=COUNTIFS(A1:A9;1;B1:B9;"<="&9)
Works only as the upper limit. If I give the third criteria range and condition as ">="&1 it does not work - returns 0.
Gasper
Where the data is in A1:B9, using a lookup table in D1:E10 with letters A-J in column D and numbers 0 to 9 in column E and the following formula in B11 referencing letters entered in A11 and A12:
=COUNTIFS(A1:A9,1,B1:B9,">="&VLOOKUP(A11,$D$1:$E$10,2,FALSE),B1:B9,"<="&VLOOKUP(A12,$D$1:$E$10,2,FALSE))
works, changing the letters in A11 and A12 gives the correct count according to what they correspond to in the looku in D1:E10.
When you say give third criteria range do you mean:
=COUNTIFS(A1:A9;1;B1:B9;"<="&9,B1:B9;">=1")
If so then try:
=COUNTIFS(A1:A9;1;B1:B9;AND("<="&9,;">=1"))
ie have two conditional ranges with the second range having both conditions combined with AND()
Maybe what you want(ed) is:
=COUNTIFS(A:A;1;B:B;">=1";B:B;"<=9")
Almost there. I noticed that three criteria ranges and conditions work only if I use "=" sign in a condition. As soon as I use
=COUNTIFS(A1:A9;1;B1:B9;"<="&9,B1:B9;">=1")
it returns 0. My goal is to eventualy replace the number in a condition with a VLOOKUP command, so the final equation should be smth like
=COUNTIFS(A1:A9;1;B1:B9;"<="&VLOOKUP(...),B1:B9;">=VLOOKUP(...)")
But the "<" and ">" signs mess with this. Still looking for a solution.
This is my entire line, if it offers any further indication. The AND() commands is at the end - and it still results in 0
=COUNTIFS(INDIRECT(CONCATENATE("baza!$";SUBSTITUTE(ADDRESS(1;MATCH("card_type_id";baza!$A$1:$AAA$1;0);4);"1";"");"$2:$";SUBSTITUTE(ADDRESS(1;MATCH("card_type_id";baza!$A$1:$AAA$1;0);4);"1";"");"$15000"));IF(C6="računska";1;0);INDIRECT(CONCATENATE("baza!$";SUBSTITUTE(ADDRESS(1;MATCH(IF($C$4="CC_SI_klasifikacija";"building_classification_id";0);baza!$A$1:$AAA$1;0);4);"1";"");"$2:$";SUBSTITUTE(ADDRESS(1;MATCH(IF($C$4="CC_SI_klasifikacija";"building_classification_id";0);baza!$A$1:$AAA$1;0);4);"1";"");"$15000"));AND("<="&VLOOKUP($C$5;$K$203:$N$223;4;FALSE);">="&VLOOKUP($C$5;$K$203:$N$223;3;FALSE)))

Change value in a cell based on value in another cell

Searched for this but could not find a way to do it.
I would like to be able to transform a value in one cell to another value in a different cell like this:
When cells in Column A contain Y set same number cells in Column B to Male or when cells in Column A contains N set same number cells in Column B value to Female.
For instance:
A2 = Y then B2 = Male
A2 = N then B2 = Female
=IF(A2="Y","Male",IF(A2="N","Female",""))
by typing yes it wont charge taxes, by typing no it will charge taxes.
=IF(C39="Yes","0",IF(C39="no",PRODUCT(G36*0.0825)))
If you want to do something like the following example, you'd have to use nested ifs.
If percentage is greater than or equal to 93%, then corresponding value in B should be 4 and if the percentage is greater than or equal to 90% and less than 92%, then corresponding value in B to be 3.7, etc.
Here's how you'd do it:
=IF(A2>=93%, 4, IF(A2>=90%, 3.7,IF(A2>=87%,3.3,0)))

Resources