Excel with conditional logic to find last row containing 'X' - excel

I don't even know if this is possible in Excel or not. I'm needing a way to link subsequent rows with previous rows (children to a parent).
For example, Row 2 contains the parent information as denoted by an 'M' in column E.
If the parent has 3 children, rows 3,4,5, they're going to contain an 'S' or 'D' in column E.
If there's an 'S' or 'D' I need to fine the last row searching up that has an 'M' and then grab the value from the B column in that row (which is a unique ID for the parent) and insert it into a column in rows 3,4 & 5 to create a relationship between the 3 children and the one parent. There will be cases where there are no children to any number of children, and so far we've been assured that the next row "up" will always be the parent for the subsequent children rows.
So in the above, because E3 and E4 are D (daughter) of Row 2, I need to take the B2 (UID) and put it in F3 and F4. Since the next row is an M (parent) F5 is blank. Row 6 is the child of row 5, so get B5 and copy it to F6.
Column B is a formula of first 3 letters of first name and dob formatted to YYYYMMDD and isn't necessary (but will be stored) for child rows. Hope that clarifies!

Here is a solution that seems to work: the following formula would be written in E4 (entered as an array formula, i.e. ctrl-shift-enter on PC or cmd-shift-enter on Mac), then copied down as needed. It requires there to be sufficient space above it (because you look "up to three above this row", that needs to be a valid row).
=IF(NOT(E4="M"),INDEX(B$1:B3,MAX((E1:E3="M")*ROW(E1:E3)))," ")
Explanation:
=IF(NOT(E4="M") - check that this is not a "parent" (i.e. we need to do a lookup)
MAX((E1:E3="M")*ROW(E1:E3))) - see what cell has an M. the (E1:E3) produces 0 or 1
- multiply by the row number and you get an array of 0 or row number
- the MAX of this is the last row number with an M
INDEX(B$1:B3, number) - find the corresponding value in column B
- note the $ sign since the ROW is absolute row
" " - empty space when there IF is false (i.e. this is a parent)
Here is how it looked for me:

There may be a simpler way but...
=IF(F12="",IF(E13<>"M",B12,""),IF(E13="M","",F12))
looks to work.

I tend to solve this sort of situation with an extra column. Column B has your ID, and Column E has the classification. Let's say Column I (which can be hidden if the display is unhelpful) has the ID of the most recent parent. Assume we are starting at Row 2 (you'll see why in a moment). If E2 has "M", then we want the ID (B2); if it doesn't, we want to propagate the value from the previous row. This is the formula which does that:
=IF(E2="M", B2, I1)
Then in Column F, your "Link To" column, use this formula:
=IF(E2="M", "", I2)
Fill down, and that's all you need.
Updated to match newly posted image

Using your provided sample data, use this formula in cell F2 and copy down:
=IF(E2="M","",LOOKUP(2,1/(E$2:E2="M"),B$2:B2))

Related

Excel array formula to copy value based on matching criteria within group

I have the above data populated until column D (i.e. Top Priority column). I am looking for a formula to copy values in column E (top priority acc no. in column E is the desired result shown in the image).
i.e. Match Top Priority(D) to Priority(C) column , where the match is true copy the corresponding-same row for column account number value in the column E. This has to be applied in the groupings of all the Location values, in column A.
I think it would be some sought of index match array function but I'm not able to build the formula if someone can helps please. Thanks
Array (Control + Shift + Enter) Formula in E2 and copy down
=INDEX($B$2:$B$10,MATCH(D2,(IF($A$2:$A$10=A2,$C$2:$C$10)),0),1)
Formula is fine even if the data in not sorted on Location. Make sure top priority is same on all rows for a given location. Say, if you change D7 to 4, then E7 will be 800 as the formula will look for the 4th priority for the A2 location.

Excel Index & Match two columns while pulling data for a third

I am trying to create a formula which compares numerical values of two columns and when a match is found return data from a different column.
Here is the problem I am having - the formula I have come up with seems to go Row by Row - for example, if column A & B are the two columns I want to compare, with column C having the data I want if a match is made. If I have a value of 1 in row A1 and a value of 1 in B1 - it will successfuly return the data in column C.
The problem is that my numbers are jumping, the row's do not match, for example column A is 1,2,3 however column B is 1,3,2. The end result here is that I get data for the value 1, but on the mismatch for the second row I get no value.
Basically the formula I made seems to do a hard comparison based just off the two rows of each column - meaning it will only compare A1 to B1. What I really need is it to compare the ENTIRE column and disregard the rows completely
Here is the formula I have been fooling around with - this formula works if A1 and B1 match
=INDEX(M:M,MATCH($L:L,$V:V,0))
In this formula M has the data I need, while columns L and V have numerical values, I need it to not 'hard check' row by row and instead evaluate the entire column and when a match is found return the result (so if both columns have a '2' return that value REGARDLESS of the fact that the '2' may be in rows A2 and B9)
Hopefully I explained my issue well, and I appreciate all the help I can get
EDIT
Sorry for failing to explain it properly on my end -- I will base my explanation off the picture link below.
I need data from column B to show up in column D. What is happening is row 2 matches so the data is successfully retrieved for number 1 - however on row 3 where column A switches the numbering it compared the number '3' to the number '2' and recognizes it is not a match and returns NA -- even though there IS a match in columns A4 and C2 -- in this situation for C3 I would need the data from B4 to showup in D3
http://i.stack.imgur.com/nrKJp.png
Two formulas that will each give you the return you want, Put one of the following in D2 and copy down:
=VLOOKUP(C2,A:B,2,FALSE)
OR
=INDEX(B:B,MATCH(C2,A:A,0))

How to increment duplicate values from 1 to n by criteria?

let's say I have a list of duplicate values in a column such as:AAABBAACABC. ...AAC. Then I want Excel to Rank respectively the first A as A1,the first B as B1 the second A as A2 and the second B as B2 and so forth until the A(nth) and the B(nth). And any additional A should automatically Rank as A(nth+1). Same thing for B C etc.
note that I should be able to enter any new value such as X that will automatically Rank as X(nth+1) or X1 if it did not exist in the table before.
Hoping that I made myself understood. I am waiting for anyone to help me. VBA or array formulas I have no preference once I can realise what I want.
Note that I found a way by sorting them in accending order but it can't match for any additional value at the end of the data base.
Thanks.
I am assuming your data is in Column "A". Enter the formula in Column "B" and copy the formula till the row you want.
=A1 & IF(A1="","",COUNTIF($A$1:A1,A1))

MS Excel formulae to Concatenate on the basis of string content

How to write the formula for below sample problem statement-
Jack1
Jill1
Mike1
Mike2
Mike3
Dave1
Dave2
Max1
This should be written as-
Jack1
Jill1
Mike1,2,3
Dave1,2
Max1
In order to manipulate data, that data needs to be relatively uniform (the less uniform it is, the more work is required to make it uniform before it can be manipulated.
In this case, I will make the following assumptions about the uniformity of this data:
(1) The number value will always exist;
(2) The number value will always be at the end of each name;
(3) The number value will always be a single digit;
(4) The number value will always start at 1; and
(5) The names will always be in order.
If any of these assumptions is false, then a VBA solution is required. If they are accurate, then a few helper columns will allow for an Excel formula solution.
Assuming your data is in column A, starting at A1, first use this formula in column B, starting at B1 and dragged down:
=right(A1)
This pulls the right-most character from each cell in column A. Then put the following formula in column C, starting at C2 and dragged down [C1 will need to be changed to just "=A1"]:
=IF(B2=1, A2, C1&","&B2)
This will create an arranged list that counts up at each row, until there is a new name. To use this to create a list which only shows the data you want, there are shorter ways but I'll show one of the simpler (but longer) methods:
In D1 and dragged down, put the following formula:
=IF(B2=1,COUNTIF($B$1:B1,1),"")
This will create a column which increases by 1 each time a new name ends its iteration.
Then in column E (or on a new sheet, or wherever you want your final list to be) put this starting in row 1 and drag down:
=IF(ROW()<=MAX(D:D),INDEX(C:C,MATCH(ROW(),D:D,0)),"")
This checks the highest number achieved in column D (ie: the number of names so far), and pulls the index in column C (the formatted name) which matches column D for the current row number. ie: if this formula is in ROW 5, and there are at least 5 names listed from column D, then it will match the number 5 from column D, and pull the info from column C where that row matches.

extract the row number of a function result

I have a simple table with 5 names and 5 grades if you will.
In another column I order the grades using the LARGE function.
Now is there a way to know the row of each of the "ordered" grades to obtain something like that?
White 23 31 5
Red 15 23 1
Green 23 23 3
Blue 18 18 4
Grey 31 15 2
The column I can't calculate is the last one!
You should use the rank() function if you want to rank these grades. Not large().
=RANK(D2,$D$2:$D$6,1)
You can try this
=MATCH(LARGE(B1:B5,1),B1:B5,0)
The result is a number of row...
In Cell D1 Put =INDEX($A$1:$A$5,MATCH(C1,$B$1:$B$5,0))
Then in Cell D2 put =IF(D1<>INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)+MATCH(C2,INDIRECT("$B$"&MATCH(C2,$B$1:$B$5,0)+1&":$B$5"),0)))
This will also work when duplicate Grades are present
But I Strongly Suggest using Sort as Follows:
*****Also: ***** Here is the explanation on the above Formulas.
To get the Row that contains the Number we are looking for (the number in Column C) you need yo use the Match() Function. We enter =MATCH(C1,B1:B5,0) in D1:
What this is doing: IS looking to the value in C1, this is 31
It is looking in Range("B1:B5"), And 0 is for an Exact match.
So when look for a match to C1 or 31 we get 5. This tells us that 31 is in Row 5
Now, to get the Value of Column A on Row 5 we use INDEX() Function as Follows:
We add to the =MATCH(C1,B1:B5,0) in D1 as =INDEX(A1:A5,MATCH(C1,B1:B5,0))
This will look in Range("A1:A5") for Row 5 (This is because =MATCH(C1,B1:B5,0) = 5)
And the result will be Grey
Now if we drag this formula down we will find the first problem:
Here are our 2 Issues:
1) We get an `N/A` error in the last row.
2) Although `Green` is only in `Range("A1:A5")` one time we see it twice
even though it would seem that `White` should be twice.
These are cause because:
1) We need to add `$` to the range that will remain the same so when we drag down
the formula is won't shift the range. As is the formula in `D5` is
`=INDEX(A5:A9,MATCH(C5,B5:B9,0))` and we receive the error *because*
`Range("A5:A9")` does not contain `15`, but the issue is we meant
to look in `Range("A1:A5")`
So we change the Formula as so: =INDEX($A$1:$A$5,MATCH(C1,$B$1:$B$5,0))
Take note that we do not use the $ on C1 in the formula cause we WANT this value to change as we move down.
But we still have the issue of double values when they shouldn't be there.
Because D1 is the first cell we won't change the formula in it. As anything that is equal to the greatest value is simply tied with it and I don't see any reason why the order of the tie would matter.
Instead we will start in D2 and enter =IF(D1<>INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)+MATCH(C2,INDIRECT("$B$"&MATCH(C2,$B$1:$B$5,0)+1&":$B$5"),0)))
What this is doing is checking if the value of =INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0))
is not equal to the value in the row above. (being a sorted list means all double values would be on top of each other) and If it is NOT the same then use the value, but if it is the same we need to do a little more work.
If the value is not the same we use the Formula INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)+MATCH(C2,INDIRECT("$B$"&MATCH(C2,$B$1:$B$5,0)+1&":$B$5"),0)))
Now to explain it I will use our example of double values. In D3 we find the formula: =IF(D2<>INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)+MATCH(C3,INDIRECT("$B$"&MATCH(C3,$B$1:$B$5,0)+1&":$B$5"),0)))
And because we know that INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)) will be equal to the above cell (White), and we have gone over how the if true works, I will focus on the if false value of: INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)+MATCH(C3,INDIRECT("$B$"&MATCH(C3,$B$1:$B$5,0)+1&":$B$5"),0))
We know MATCH(C3,$B$1:$B$5,0) is the Row that contains the first instance of C3 in this case 23 and the row is Row 1 so we need to look for 23 in the row Under Row 1. So we use MATCH(C3,INDIRECT("$B$"&MATCH(C3,$B$1:$B$5,0)+1&":$B$5"),0) which is equal to MATCH("23", B2:B4,0) because we are adding a 1 to the row that has the 1st match for 23 or C3.
that will now return us the Value of 2 as, the value 23 is in the second row of Range("A2:A5"), Red is in Row 1 and Blue in Row 3 of that range as shown:
but we don't want Row 2 we know that 23 relates to Green and that Green is in Row 3 So we add the row the we last found the value 23 (1 or MATCH(C3,$B$1:$B$5,0))to the row we currently found it (2) and get Row 3.
Here is a formula approach based on the methodology outlined in this link. The final layout of this approach is shown below.
I have assumed that there is 1 header row and I use 2 helper columns (D & E). While additional rows can be added to the header, the table must begin in column A in order for the formulas in column E to work correctly.
Although the helper columns could be eliminated by consolidating their formulas into the formulas in column F, I do not recommend it: the resulting formulas would be a pain to maintain.
Formulas Needed
Cell C2: =LARGE(B:B,ROW(A2)-ROW($A$1)) [Copy down to bottom of data]
Cell D2: =MATCH(C2,B:B,0) [Copy down to bottom of data]
Cell E2: =D2
Cell E3: =IF(D3<>D2,D3,E2+MATCH(C3,INDIRECT("B"&(E2+1)&":B"&COUNTA(A:A)),0))
[Copy down to bottom of data]
Cell F2: =OFFSET($A$2,E2-ROW($A$2),0) [Copy down to bottom of data]
Explanation of Answer
There are four steps to getting the answer:
Sort the grades from highest to lowest (as you showed in your example data)
Create a partial ordering of the row numbers for the sorted grades
Get the row numbers for duplicate grades
Use that ordering to show the name for each sorted grade
Sort the grades from highest to lowest
As you have done, my sort uses the LARGE function, which returns the nth largest value in a range or array. As shown, the LARGE function in cell C2 takes the grades in column B. The "n" for LARGE is calculated as the current row number minus the number of rows in the header, in this case the 1 row for cell A1. When the formula is copied down, "n" progresses from 1 to 2 to 3, etc.
Partially order the grade row numbers
The next step is to determine the row numbers for the unsorted grades that correspond to the sorted grades.
To do that, I use the MATCH function to find where each of the sorted grades lies in the list of unsorted grades in column B. MATCH takes three arguments--the value to be matched, the range in which to make the match, and optionally, the type of match, with a value of 0 or FALSE for an exact match--and returns an index number which represents where in the lookup range the match is found (1 for the first row in the match range, 2 for the second row, etc.).
In the formula for cell D2 shown above, the MATCH function on the grade 31 returns 6 since 31 is in the sixth row of column B.
The result for cell D4 shows why it is only possible to get a partial ordering with this formula. While we are trying to lookup the row for the second instance of a grade of 23, the formula returns a value of 2, which corresponds to the row for the first instance of 23. That's because MATCH will always return the first match for 23 it finds, which is on row 2!
Get correct row numbers for duplicate grades
The next step is to get the correct row references for the duplicated row numbers in column D. The formulas that accomplish this are shown for the first three cells in column E of the table.
There are three cases that have to be dealt with in column E:
For the first (and possibly only) instance of the highest grade, it is possible to just use the row number calculated in cell D2.
The second case deals with the first instances of the row references of the remaining grades. For these the rows numbers calculated in column D can again be used (via the TRUE branch of the IF statement in the column E formulas). For example, in cell E2 -- which corresponds to the first instance of grade 23 -- the row number in cell D3 can be used.
The final case is the rows for duplicate grades. Here, the MATCH for each duplicate in column B is recalculated using a sliding range that excludes the previous matches for that grade. For example, for the duplicated grade of 23 in column C, the match is on the range B3:B6, rather than the range of B2:B6 used in the column D calculation.
Diplay the names in sorted order
This final step is straight forward: Get the name corresponding to the sorted grade. Here the OFFSET function is used; its arguments are a cell reference and the number of rows and columns from that reference that the desired value is to be found.

Resources