An arrayformula to find the previous non-empty cell in another column and find the last row to work with

I would like to do something similar to what was solved in the question:

An arrayformula to find the previous non-empty cell in another column

The problem was:

If I have a column "input", I would like the column "ouput" to be filled with the preceding value of the "input" column.

``` input - ouput a a a a b b b b b c c c c c ```

It was a great answer, however, the ArrayFormula works with an user-specified range:

``````=ArrayFormula(vlookup(ROW(1:10),{IF(LEN(A1:A10)>0,ROW(1:10),""),A1:A10},2))
``````

How would I make the function to automatically get the last row that it has to take?

Specifically, I would like to know how to get automatically the number 10 of the last function.

I have tried to use INDIRECT but it finds an error.

``````=ArrayFormula(vlookup(ROW(INDIRECT("B3:B"&INDEX(SORT(B:B,ROW(B:B),FALSE),1))),{IF(LEN(INDIRECT("B3:B"&INDEX(SORT(B:B,ROW(B:B),FALSE),1))>0,ROW(INDIRECT("3:"&INDEX(SORT(B:B,ROW(B:B),FALSE),1)),""),INDIRECT("B3:B"&INDEX(SORT(B:B,ROW(B:B),FALSE),1)},2)))))
``````

This part: `INDIRECT("B3:B"&INDEX(SORT(B:B,ROW(B:B),FALSE),1))` is used to get the last value of a column.

Any help would be appreciated.

• What was your INDIRECT attempt? What was the error, exactly?
– ale
Mar 1, 2016 at 17:31
• I've just updated the question to present it @AlE. Mar 1, 2016 at 18:06
• Why not just refer the entire column? E.g., `=ArrayFormula(vlookup(ROW(B3:B),{IF(LEN(B3:B)>0,ROW(B3:B),),B3:B},2))`
– user79865
Mar 1, 2016 at 21:54
• @Sally using open ended references will fill the entire column. Considering the functions used it's not a big deal. Anyway, I added an answer including ARRAY_CONSTRAIN to limit the number of rows returned.
– Rubén
Mar 2, 2016 at 23:28

``````=ArrayFormula(if(row(B3:B) <= max(if(not(isblank(B3:B)), row(B3:B))),vlookup(row(B3:B),filter({row(B3:B),B3:B},len(B3:B)),2),))
``````

should bring you the output you expected.

• Thank you, that was it, your solution is the best because it doesn't need to specify the last value of the column and it also leaves the unmodified cells as blanks. Jan 16, 2017 at 15:12

The following formula automatically fills the output column and stops at the row 10. To fill more rows, replace 10 by the desired number of rows or by a function / subformula that calculates the required rows.

```=ArrayFormula(
ARRAY_CONSTRAIN(
VLOOKUP(ROW(A:A),{IF(LEN(A:A)>0,ROW(A:A),""),A:A},2),
10,
1
)
)
```
• I tried entering the part INDIRECT("B3:B"&INDEX(SORT(B:B,ROW(B:B),FALSE),1)) instead of the 10 but it didn't work. Jan 16, 2017 at 15:13