Return list of matching rows - excel

Sheet1
A B C D
1 2 3 4
2 4 5 6
3 3 5 6
4 7 3 1
5 4 6 7
Sheet 2
A B C D
1 4
2 1
3 1 3 4
4 1 2 5
5 2 3
6 2 3 5
7 4 5
Column A of Sheet 2 has a non repeating listing of all values in Sheet 1. I would like a forumla so Col B of Sheet 2 lists the value of Sheet 1 Col A for each row where the Sheet 2 Col A lookup value is found. Either giving me a comma sep list or putting the results in sheet 2 Col B,C,D,...

I came up with a solution, but it involves a handful of formulas, not just one. Hopefully, you will still find it useful.
Your Sheet2 would basically look like this:
Here are the formulas you need to enter:
B1: =IFERROR(SMALL($G4:$U4,COLUMN()-1),"") [drag down and across to D7]
G1: =IF(F2=$W2,F1+1,MAX(1,F1)) [drag across to U1]
G2: =IF(F2=$W2,1,F2+1) [drag across to U2]
G3: =INDEX(Sheet1!B$1:D$5,G1,G2) [drag across to U3]
G4: =IF(G$3=$F4,G$1,"") [drag down and across to U10]
What this does is organize the Sheet1 data into rows, where each row corresponds to a lookup value. Then, it grabs the smallest value, the second smallest value (if one exists), and third smallest value (if one exists) from each row.
This should be easy to scale to as large a spreadsheet as you need (as long as you don't run out of columns).

Related

Excel formula to return last xth numeric value from row

I have a data set like this:
a 1 2 3 5 6 7
b 5 1 3 4
c 7 4 3 2 6
d 1 2 3 7
e 7 5 5 6 7
which i want to transform in excel to only show the last 4 numeric values for each row, i.e.
a 3 5 6 7
b 5 1 3 4
c 4 3 2 6
d 1 2 3 7
e 5 5 6 7
Ive managed to use =INDEX(row1,MATCH(9.99999999999999E+307,row1)) which correctly returns the last value (i.e. 7 for row1 in this case) but how could i get the 2nd last, 3rd last, 4th last etc?
With a in A1, in M1 copied across and down to suit:
=MID(RIGHT($B1&$C1&$D1&$E1&$F1&$G1&$H1&$I1&$J1,4),COLUMN()-12,1)
then hide ColumnsB:L.
(Only works for single digits, as shown. Recent versions of Excel may offer better options.)
So I managed to figure out how to do this generically for any digits:
Say your original numerical data is in cells D5:N9.
OPTIONAL:
If any of the rows of data contain the same value twice create a copy in cells D13:N17 which amends duplicates slightly by using in D13 and dragging to fill
=IF(D5="","",D5+0.0001*(COUNTIF($D5:D5,D5)-1))
Then in cells A13:A17 enter values 13,14,15,16,17
and in cells A20:A29 enter values D, E, F, G, H, I, J, K, L, M
In Cell D20 you can return the last from row 13 value using:
=LOOKUP(9.99999999E+307,D13:N13)
and copy this down for each row.
In Cell E20 you can return the 2nd last entry using
=LOOKUP(9.99999999E+307,$D13:INDIRECT(CONCATENATE(INDEX($A$20:$A$29,MATCH(D20,$D13:$N13,0)-1),$A13)))
and copy this down for each row and drag it across to H24 to fill in all the values for 3rd last, 4th last and 5th last for each row.

Summing a set of values based on cell content and row position

In the below table rows 2,3 and 4 have some details of a sporting event.
Range A2:C4 has a set of squad numbers and range D2:F4 has the details of who scored goals.
A B C D E F
1 Squad # Scorers
2 1 3 6 2 8 3
3 3 6 7 6 1
4 1 5 6 7 2 4
As an example squad # 6 has scored 8 goals based on the values equivalent position in the Squad section and relative to the Scorers range.
What formula will give me this total value and the equivalent total values for all squad numbers like the table below?
Some cells will be empty like F3.
1 9
3 14
5 2
6 8
7 0
A simple SUMPRODUCT function should take care of this.
=SUMPRODUCT($D$2:$F$4, --($A$2:$C$4=H2))
Fill down as necessary.
        
With only 3 games, the simplest way that I can see to do this would be to use 4 SUMIFS statements, as follows [assumes that your results data is in columns A & B, starting at row 5 and moving down; A5:A12 would hold the IDs for the different squads, and this formula would go in B5 and be copied down]:
=SUMIFS($D$2:$F$2,$A$2:$C$2,A5)+SUMIFS($D$3:$F$3,$A$3:$C$3,A5)+SUMIFS($D$4:$F$4,$A$4:$C$4,A5)
What it does is sum columns D:F, for row 1, row 2, and row 3, based on the fact that columns A:C for those rows match the appropriate squad ID.
This would be possible with an arguably shorter but more complex Array Formula, but with the data as you've presented it, I feel this is the clearest method.

Sorting row by row

I have a table in excel which has values like below
2 1 3
6 5 4
7 2 3
Considering the rows do not have a column header, what is the easiest way to sort them row by row, such that I get the below output
1 2 3
4 5 6
2 3 7
If the array starts in A1 please try (UNTESTED) in AB1 copied across 27 columns and down to suit:
=SMALL($A1:$AA1,COLUMN()-27)

Highlight duplicate cell values in different colours

I'm using conditional formatting to highlight duplicate cells, but I have up to 5 instances of a duplicated value. How can I apply a separate colour to each duplicated cell?
So if I have 5 instances of ABC123, how can the first instance be red, the second green, the third blue, fourth yellow, fifth orange (or whatever colours)?
I take it this is in Excel 2007? In earlier versions you can only have three different conditions.
The way I'd do this is to introduce a 'helper' column, containing a formula which provides the count of duplicates up to that row number:
Say you have your list (including dupes) in range A1:A10, and in column B1:B10, a formula of the form =COUNTIF($A$1:A1,A1) through =COUNTIF($A$1:A10,A10), you'd have
A B
------------
1 4 1
2 2 1
3 4 2
4 3 1
5 1 1
6 2 2
7 4 3
8 2 3
9 2 4
10 1 2
I don't know your exact Conditional Formatting requirements, but you can now use 5 nested IF formulas to take account of not only the fact that something is a dupe (cell in B is >= 2), but also the count of the dupe.

Compare two columns in excel

Hey, I have an excel document with columns A and B. Row A has 2595 values. Row B has 411 values.
I'm trying to figure out how I can compare these two columns by marking column C with the number of times we find a value(s) from row a.
For Example
COLUMN A COLUMN B COLUMN C
1 1 1 (because we have one value of 1 from column A)
2 2 1 (because we have one value of 2 from column A)
3 3 2 (because we have 2 threes from column A)
3 4
4 5
5 6 2 (because there are two 6's in column A)
6 7 0 (because there are no 7's in column A)
6
I'm sure you can see where I'm going with this but for the life of me I cannot figure out how to do this, I've been searching around all morning. Help!
If needed I can supply the excel document.
You can use the COUNTIF function
Column A Column B Column C Answer
1 1 =COUNTIF($A$2:$A$9,B2) [1]
2 2 =COUNTIF($A$2:$A$9,B3) [1]
3 3 =COUNTIF($A$2:$A$9,B4) [2]
3 5 =COUNTIF($A$2:$A$9,B5) [1]
4 6 =COUNTIF($A$2:$A$9,B6) [2]
5 7 =COUNTIF($A$2:$A$9,B7) [0]
6
6
You can use COUNTIF. If A1:A8 is your search criteria, B1 is the value you're currently processing, C1 would be:
=COUNTIF(A1:A8;B1)

Resources