Excel - Offset to last non-blank cell - excel

From my research, when a bunch of cells are merged, you can only reference the first row and first column of the merged cells. EG. if A1:A3 are merged, then I can only access the data using A1 only, and A2 and A3 returns 0.
Now let's say I have a column B that has a formula that calculates based on values in column A. If I drag this formula down, then B2 and B3 will end up using value of 0, when they should be using value in A1.
Effectively, what i want to do is "if the cell in column A (of this row) is blank, then use the last non-blank value going upwards".
I know this will need to combine a couple of formulas, but I can't figure out how to create this. For a start, I can use the Offset function to "go up", but the difficult part here is how to find the previous non-blank cell?
I also tried combing OFFSET with COUNTA (see https://www.exceltip.com/other-qa-formulas/get-the-value-of-the-last-non-blank-cell-in-a-column-in-microsoft-excel.html), but this doesn't work if this occurs multiple times.

Easiest way is to use a helper column:
In B2 write
=IF(NOT(ISBLANK(A2)),0,B1+1)
and in C2 write
=OFFSET(A2,-B2,0)
Edit: actually... the solution without helper column is even easier! Write in B2:
=IF(ISBLANK(A2),B1,A2)

To avoid the helper column, you can use the INDEX + AGGREGATE functions:
=INDEX($A$1:A1,AGGREGATE(14,6,($A$1:A1<>"")*ROW($A$1:A1),1))

Related

Referencing parameter lookup_array in function match in excel

I want to use the match function in Excel the following way: match(True, D2:J2>=K1,0), to find the smallest value that is greater than or equal to lookup_value. My problem is how to change the row number in the array, since the row number is on his part the result of a match function.
Thanks for the help in advance!
Your baseline formula is:
=MATCH(TRUE,D2:K2>=K1,0)
which looks at row #2:
To change that row, lets put 7 in cell A1 to mean use row #7. We change the formula to:
=MATCH(TRUE,INDEX(D:D,A1):INDEX(K:K,A1)>=K1,0)
So the value in A1 defines the row that MATCH() should use.
EDIT#1:
Here is another example of using INDEX() to return a cell reference.
Say we have data in column A. We want the user to be able to select parts of the column (start and stop rows) and sum the part. However, we don't want the user to tinker with the formula. In fact we are going to lock the formula cell so the user can't destroy the formula. We tell the user to put the start row in D1 and the end row in D2.
We put the formula in cell D3:
=SUM(INDEX(A:A,D1):INDEX(A:A,D2))

Drag formula down and change the reference of the column (index+match)

I need help with the following formula:
=INDEX(Sheet2!A2:A11,MATCH(Sheet1!Q5,Sheet2!C2:C11,0)+0)
(this part needs to change column references: Sheet2!C2:C11,0)+0)
I need to change the column reference whenever I'm dragging it down. I tried this:
=INDEX(Sheet2!$A$2:$A$12,MATCH(Sheet1!Q4,OFFSET(Sheet2!$A$2:$A$12,0,ROW(O$4:O4)-1),0)+0)
but it always comes up with #N/A
I tried solution from other topics but couldn't find one that uses index and match.
PS. My formula starts from cell O5
Can you advise please?
Much obliged
In general, if you want to change the column reference when dragging down, use a combination of INDEX and ROW, e.g.
= INDEX($1:$1,ROW())
This will grab values further to the right in the first row as the formula is dragged down.
You can also modify this to have INDEX return a range (instead of just a single cell) to be used as part of another formula, e.g.
= INDEX($1:$5,0,ROW())
This returns a 5x1 array which shifts over to the right as the formula is dragged down. (The 0 in the above formula indicates to select all of the rows in the $1:$5 range.)
In your formula, you can try replacing this:
Sheet2!C2:C11
With this:
INDEX(Sheet2!$2:$11,0,ROW()+<offset>)
Where <offset> is the necessary offset that you need.
If your formula starts in O5 and you want that first formula to grab the C column, I imagine that <offset> should be -2. This is because ROW() of O5 is 5, but you want that cell to grab the 3rd column (so you need to subtract 2). Then when you drag down to O6, that part of the formula would evaluate to Sheet2!D2:D11, and in cell O7, it would evaluate to Sheet2!E2:E11, etc.
So your final formula should be:
= INDEX(Sheet2!A2:A11,MATCH(Sheet1!Q3,INDEX(Sheet2!$2:$11,0,ROW()-2),0)+0)

Excel: Search for text-string in a row, return value of adjacent column cell (columns will differ frequently)

I have tried to use VLOOKUP, HLOOKUP, LOOKUP, INDEX and MATCH but haven't so far been able to solve the problem. I want to fill column A with the value adjacent to the value left of STRING that might be found in each row. The column of interest frequently differs.
E.g.
Return value of F1 if G1=STRING, F2 if G2=STRING, E3 if F3=STRING, D4 if E4=STRING
Picture
I managed to solve it by:
=IFERROR(INDEX($S2:$BJ2, MATCH("STRING", $S2:$BJ2,0)-1),"")
Now, if I want to extract the 2nd, 3rd and 4th value (if it happens to be more than one match) adjecent to "STRING" which changes have to be made?
I'm going to assume your data is in E1 to H100 - adjust as you need.
First we need the row -
SUMPRODUCT((E1:H100=A1)*ROW(E1:H100))
Then we use the same trick to get the column
SUMPRODUCT((E1:H100=A1)*COLUMN(E1:H100))
This gives us the row and column of the text
Now we use this to find the text in the next column
As the row and column are offset from cell 0,0 and A1 is 1,1 we need to subtract 1 from the row, and then 2 from the column to make it 1 cell to the left
This leaves us with the following formula:
=OFFSET(A1,SUMPRODUCT((E1:H100=A1)*ROW(E1:H100))-1,SUMPRODUCT((E1:H100=A1)*COLUMN(E1:H100))-2)
don't forget to anchor points if you go dragging the formula to fill multiple cells

A simple voting mechanism in excel

I have an excel sheet which has three columns. These three columns can have some value or be blank. I have to update a fourth column based on the entries in these three columns. The criteria for updating the fourth column is as follows:
If two or more columns have same value then fourth column will have
this same value.
If all three values are different then fourth column will have first
non blank value.
If there is only one non blank value then fourth column will have
that non blank value.
I'm unable to figure out what should be the formula in fourth column for this criteria.
Here is an example
=IFERROR(INDEX(A2:C2,,MODE(MATCH(A2:C2,A2:C2, 0))), IF(A2="", IF(B2="", C2, B2), A2))
Just paste it to D2 and drag down.
To explain it, it looks for the MODE (Most common value) of everything in the range A2:C2.
MODE(MATCH(A2:C2, A2:C2, 0))
This would return the first position in which the most common value returns, in this case we use the INDEX function and get the value at the given index, in the range A2:C2.
INDEX(A2:C2,,MODE(MATCH(A2:C2,A2:C2, 0)))
This part so far is slightly based on code from here
Now this will give us the letter, but only if one letter is more common than the others, if they all have different values, then it'll return an error. In this case, we use the IFERROR function which, if there is an error in it's first argument, will instead calculate the second argument. So if there isn't a match, then we want to just get the first non blank cell.
So check if A2 is blank, if it is check if B2 is blank, if it is, use the cell value in C2, otherwise use the cell value in B2, but if A2 is not blank, use the value in A2.
IF(A2="", IF(B2="", C2, B2), A2))
With a little bit of working around, you could probably turn the above statement into something that's a little more expandable (google first non-blank value in row or something like that).
Hope this helps.
Using your example, in cell D2 and copied down:
=IF(COUNTA(A2:C2)=0,"",IF(MAX(INDEX(COUNTIF(A2:C2,A2:C2),))>1,INDEX(A2:C2,MATCH(MAX(INDEX(COUNTIF(A2:C2,A2:C2),)),INDEX(COUNTIF(A2:C2,A2:C2),),0)),INDEX(A2:C2,MATCH(TRUE,INDEX(A2:C2<>"",),0))))

Excel: Find intersection of a row and a column

My question is how can I find an intersecting cell of a specific column and row number?
My situation is this: with some calculations I find two cells, lets say B6 and E1. I know that I need a row of the first one and a column of the second one. So I could just use ROW and COLUMN functions to get the numbers. After that, I need to find an intersecting cell. Which would be E6 in this example.
I would just use INDEX(A1:Z100;ROW;COLUMN) but I don't know the exact area that I'm going to need - it depends on other stuff. I could use something like A1:XFG65000 but that is way too lame. I could also use a combination of INDIRECT(ADDRESS()) but I'm pulling data from a closed workbook so INDIRECT will not work.
If this would help to know what is this all for - here's a concrete example:
I need to find limits of a section of a sheet that I would work with. I know that it starts from the column B and goes all the way down to the last non-empty cell in this column. This range ends with a last column that has any value in first row. So to define it - I need to find the intersection of this last column and the last row with values in B column.
I use this array formula to find the last column:
INDEX(1:1;MAX((1:1<>"")*(COLUMN(1:1))))
And this array formula to find the last row:
INDEX(B:B;MAX((B:B<>"")*(ROW(B:B)))
Last column results in E1 and last row results in B6. Now I need to define my range as B1:E6, how can I get E6 out of this all to put into the resulting formula? I've been thinking for a while now and not being and Excel expert - I couldn't come up with anything. So any help would really be appreciated. Thanks!
You can use an Index/Match combination and use the Match to find the relevant cell. Use one Match() for the row and one Match() for the column.
The index/match function to find the last cell in a sheet where
column B is the leftmost table column
row 1 is the topmost table row
data in column B and in row 1 can be a mix of text and numbers
there can be empty cells in column B and row 1
the last populated cell in column B marks the last row of the table
the last populated cell in row 1 marks the last column of the table
With these premises, the following will return correct results, used in a Sum() with A1 as the starting cell and Index to return the lower right cell of the range:
=SUM(A1:INDEX(1:1048576,MAX(IFERROR(MATCH(99^99,B:B,1),0),IFERROR(MATCH("zzzz",B:B,1),0)),MAX(IFERROR(MATCH(99^99,1:1,1),0),IFERROR(MATCH("zzzz",1:1,1),0))))
Since you seem to be on a system with the semicolon as the list delimiter, here is the formula with semicolons:
=SUM(A1:INDEX(1:1048576;MAX(IFERROR(MATCH(99^99;B:B;1);0);IFERROR(MATCH("zzzz";B:B;1);0));MAX(IFERROR(MATCH(99^99;1:1;1);0);IFERROR(MATCH("zzzz";1:1;1);0))))
Offset would seem to be the way to go
=OFFSET($A$1,ROW(CELL1)-1,COLUMN(CELL2)-1)
(The -1 is needed because we already have 1 column and 1 row in A1)
in your example, =OFFSET($A$1,ROW(B6)-1,COLUMN(E1)-1) would give the value in E6
There is also ADDRESSS if you want the location: =ADDRESS(ROW(B6),COLUMN(E1)) gives the answer $E$6
The following webpage has a much easier solution, and it seems to work.
https://trumpexcel.com/intersect-operator-in-excel/
For example, in a cell, type simply: =C:C 6:6. Be sure to include one space between the column designation and the row designation. The result in your cell will be the value of cell C6. Of course, you can use more limited ranges, such as =C2:C13 B5:D5 (as shown on the webpage).
As I was searching for the answer to the same basic question, it astounded me that there is no INTERSECT worksheet function in Excel. There is an INTERSECT feature in VBA (I think), but not a worksheet function.
Anyway, the simple spacing method shown above seems to work, at least in straightforward cases.

Resources