Google Sheets – How to Autofill Until Next Non-Blank Cell or Value Change

formulasgoogle sheetsvlookup

I'm trying to format a CSV file that contains a huge list of products (8000+) that are on separate rows but need all the fields to be on the same row. The trouble is that some of the cells are intentionally left blank, so I'd want to stop autofilling once the product ID changed, then resume the autofill once it reached the next product that also had a nonblank cell. I've tried a few things:

=ARRAYFORMULA(VLOOKUP(ROW(W:W),{IF(LEN(V:V),LOOKUP(UNIQUE(V:V),V:V,ROW(V:V))),FILTER({ROW(W:W),W:W},W:W<>"")},2,TRUE))

error but counting the number of changes correctly

=ARRAYFORMULA(IF(LEN(V:V),LOOKUP(UNIQUE(V:V),V:V,ROW(V:V),VLOOKUP(ROW(W:W),FILTER({ROW(W:W),W:W},W:W<>""),2,TRUE))))

error but showing FALSE in the right places
=ARRAYFORMULA(IF(LEN(V:V),LOOKUP(UNIQUE(V:V),V:V,VLOOKUP(ROW(W:W),FILTER({ROW(W:W),W:W},W:W<>""),2,TRUE))))

returning some values but in a strange order

Mockup of file: https://docs.google.com/spreadsheets/d/1DAIDk5EphV-RPGzo4Um4k0RZn0A2uhsTiPUApDzafRg/edit?usp=sharing (in this case, it would check when the value of column A changes and stop copying the nonblank cell values of column B)

Any suggestions would be much appreciated!

Best Answer

Put this formula in row 2 of an empty column:

=arrayformula( 
  iferror( 
    vlookup( 
      A2:A, 
      unique(A2:B), 
      column(A2:B) - column(A2) + 1, 
      false 
    ) 
  ) 
)

The formula will grab the first value in column B in each range of column A where a new value appears for the first time.


OUTPUT

snapshot

Related Topic