Sum of columns whose header appears in a lookup table - excel

So i have the main data table:
ColA ColB ColC ColD
aa 1 0 1
bb 1 2 2
cc 1 2 3
Row aa, bb, cc, etc. The total number of rows shouldn't be larger than a couple of hundred .
A second sheet\collumn has a table that list only the relevant column's helper, adaptable on the fly:
Helper
ColB
ColD
The search helper tells the functions which columns i should use in the search.
The idea is to sum all values from columns that are refered in the Helper table and that match the unique identifier in "ColA".
So the result would, using the above Helper table:
Identifier aa, would return 2.
Identifier BB, would return 3.
Identifier CC, would return 4.
Any idea how to do it.

You can do this with VLOOKUP, IF and SUM:
IF(VLOOKUP(C1,$H1:$H99,1)=C1, SUM(C2:C99), "")

Enter this formula with Control+Shift+Enter to make it an array formula.
=SUM(($A$2:$A$4="aa")*((($B$2:$B$4)*NOT(ISNA(MATCH($B$1,rngHelper,FALSE))))+(($C$2:$C$4)*NOT(ISNA(MATCH($C$1,rngHelper,FALSE))))+(($D$2:$D$4)*NOT(ISNA(MATCH($D$1,rngHelper,FALSE))))))

Related

Sum of values excluding rows with duplicate criteria

I'm certain this has a simple solution but I can't find it!
Problem
I need to sum the values in COL B while excluding all but the first instance of the corresponding criteria in COL A, which may contain duplicate values.
Example Data
Record
Count
AA
1
BB
2
AA
1
CC
4
DD
7
Unique Record Count: 14
Attempted Solution
I have messing with SUMIF or SUMPRODUCT but haven't been able to work out how to also include the first instance of the corresponding value for COL A. Whatever build of Excel I have doesn't have the function =UNIQUE().
Formula in D6 is:
=SUMPRODUCT(AVERAGEIF(A2:A6;A2:A6&"";B2:B6)/COUNTIF(A2:A6;A2:A6))
Notice this will work only if all values assigned to a Record are all the same always.
You could use a helper column to get the summable values. E.g., in column C:
=IF(COUNTIF($A$2:$A2,A2)>1,0,B2)
And then do a SUM(C:C)
Example Data
Record
Count
Countable
AA
1
1
BB
2
2
AA
1
0
CC
4
4
DD
7
7

How to find the maximum of lookup values in excel

I have an excel table that looks like this
Row Column1 Column2 Column3
R1 A B C
R2 C D X
I have a table that holds the values corresponding to the entries in Columns 1 to 3 which looks like this -
Key Value
A 1
B 7
C 2
D 4
X 9
I want to create a Column4 that has the maximum looked-up value of columns 1 to 3, i.e. the result would look like this -
Row Column1 Column2 Column3 Looked_Up_Max
R1 A B C 7
R2 C D X 9
I tried writing an array formula like this -
={max(if(B1:D1,vlookup(B1:D1,lookup_table!$A$1:$B$5,2,0)))}
But it does not work. Any way to do this is one step instead of say creating three additional columns with the looked up values and then taking a max of the additional columns?
Thank you for the help
If the data on the lookup table is sorted then you can use this array formula:
=MAX(LOOKUP(B2:D2,$H$2:$H$5,$I$2:$I$5))
Being an array it needs to be confirmed with Ctrl-Shift-enter instead of Enter when exiting edit mode.
Another that does not care about sort order that uses SUMIFS()
=MAX(SUMIFS(I:I,H:H,B2:D2))
Still an array formula, but this assumes that the Key in the lookup is unique.
If not in order and not unique and the user wants the first then we need this convoluted array formula:
=MAX(IFERROR(INDEX(I:I,N(IF({1},MATCH(B2:D2,H:H,0)))),-1E+99))
Still and Array formula.

How do I split a row of text into different columns according to number of characters using a macro in Microsoft Excel?

I want to know if I can use a macro in Excel to separate data in a single column into different colums according to number of characters. For example, what I have is this in column A
A
AB
ABC
1A
564
8
What I need is this, in colums A, B and C
A AB ABC
8 1A 564
Thanks.
Use the following formula in a new column B next to Column A:
=IFERROR(INDEX($A$1:$A$6,SMALL(IF(LEN($A$1:$A$6)=1,ROW($A$1:$A$6),99999),ROW()),1),"")
Array Formula press Ctrl+Shift+Enter at the same time
and drag it down, it will write the Values of B whose Length is 1, and when it gives empty it means no more Values with Length 1
Small will find the Cell which length is 1 (Row()=1, 1st cell which length=1, Row()=2, 2nd cell which length =1 ...)
If will return all the rows for the corresponding condition
Index will return the Cell
Iferror return empty "" if no more match
For the second column write 2 instead of 1 in LEN($A$1:$A$6)=2
=IFERROR(INDEX($A$1:$A$6,SMALL(IF(LEN($A$1:$A$6)=2,ROW($A$1:$A$6),99999),ROW()),1),"")
For the third column write 3 in LEN($A$1:$A$6)=3
=IFERROR(INDEX($A$1:$A$6,SMALL(IF(LEN($A$1:$A$6)=3,ROW($A$1:$A$6),99999),ROW()),1),"")

Excel vlookup expansion

I currently have an excel worksheet with three columns
id annotation person_id
1 yes 1
1 no 2
1 yes 3
I'm trying to reformat this on another worksheet into a table that looks like:
id 1 2 3
1 yes no yes
I'm using this
vlookup: =VLOOKUP(A2,sheet2!$F$1:$G$10,2,FALSE)
where a2 is the id and f$1:g$10 is the range of data for that particular person.
At the moment I'm doing this per person and its tedious - there's thousands of people. I need a way to incorporate the person_id into my vlookup so that, if the person_id in the column header matches the person_id in sheet 2 and both ids match, then insert the annotation.
This appears to be a good candidate for an INDEX-MATCH formula, with a multi-condition MATCH() formula to account for both ID criteria. The formula below places the raw data are in A1:C4 and the reformatted data in E1:H2, so you will need to change the references:
In cell F2, I've entered {=INDEX($A$1:$C$4, MATCH(1, ($A$1:$A$4=$E2)*($C$1:$C$4=F$1), 0), 2)}, which yields
[A] [B] [C] [D] [E] [F] [G] [H]
[1] id annotation person_id ID 1 2 3
[2] 1 yes 1 1 yes no yes
[3] 1 no 2
[4] 1 yes 3
The relative references on $E2 and F$1 allow the filling of the formula across rows and columns.
To explain the formula a bit:
In the INDEX() formula, the first argument $A$1:$C$4 is the raw data "table" (array).
The second argument searches for the row where the value ($A$1:$A$4=$E2)*($C$1:$C$4=F$1) is exactly (given by the third argument, 0) equal to 1.
($A$1:$A$4=$E2)*($C$1:$C$4=F$1) is the multiplication of two TRUE/FALSE statements, which is equal to 1 when the main ID in col A is equal to the ID value in E2, and the person ID in col C is equal to the column header in F1.
The final argument, 2, is the column in the data table from which to look up the result.
You may want to consider using another MATCH() formula to dynamically identify the look-up column based on the column header; e.g., instead of 2, use MATCH("annotation", $A$1:$C$1, 0).
Important notes:
The formula needs to be entered as an array formula using Ctrl+Shift+Enter.
This method assumes the combinations of id & person_id are unique.
Also, note that this formula is clearer with named ranges: e.g. (using the suggested second MATCH() function instead of 2), {=INDEX(RawData, MATCH(1, (ID=$E2)*(PersonID=F$1), 0), MATCH("annotation", Headers, 0)}.
You could do like this
First create a new column on the first table =B2&D2
A B C D
1| id annotation person_id
2| 11 1 yes 1
3| 12 1 no 2
4| 13 1 yes 3
And then on the second table use a formula like this
=VLOOKUP(B16&C15;A2:C4;3;FALSE)
B C D F
14 1 2 3
15 1 yes no yes
16 2

Excel Data Manipulation with alphanumberic data

I have two columns in Excel. We can say that column 1 and column 2. Both columns have alphanumeric data i want to minus column 1 from column 2. How can i do that.
Example:
column 1 column two result column
ab ab ad
ac ac
ad
Thanks
There are more sophisticated/cleaner ways to do this, but I'd enter this formula into column C (update your ranges accordingly to capture each list), and then sort the result column:
=IF(COUNTIF($A$1:$A$8,$B$1:$B$4)=0,A1,"")
This compares the list in $A$1:$A$8 to the list in $B$1:$B$4 and places the "differences" in the result column C. I'm assuming that sorting is permissible. But like I said, there are VBA solutions that could do this a little more cleanly.

Resources