Search code examples
regexgoogle-sheetsgoogle-sheets-formulaarray-formulasgoogle-sheets-query

Counting words in ARRAYS with ARRAYFORMULA in Google Sheets


Counting words with Array formula for every cell won't work.

I have tried to use:

=ARRAYFORMULA(COUNTA(SPLIT(Range, " ")))

=ARRAYFORMULA(SUM(COUNTA(SPLIT(Range," "))))

Both didn't work.

I expected ARRAYFORMULA to count words for every cell in the column and put it next to it, but it counted all the text and put it in one cell instead.

Copy of the sheet (Problem in Cell D123): https://docs.google.com/spreadsheets/d/1pPbJ9k4tiLk8hVxHXRgvu6b4vxJOcgTmyFhD_g8gc_Q/edit?usp=sharing


Solution

  • =ARRAYFORMULA(IF(LEN(A3:A), 
     MMULT(IF(IFERROR(SPLIT(IF(LEN(A3:A), A3:A, ), " "))<>"", 1, 0), 
     ROW(INDIRECT("A1:A"&COUNTA(IFERROR(
     QUERY(IF(IFERROR(SPLIT(IF(LEN(A3:A), A3:A, ), " "))<>"", 1, 0), "limit 1", 0)))))^0), ))
    

    0


    also could be done by just counting the spaces:

    =ARRAYFORMULA(IF(LEN(A3:A), LEN(REGEXREPLACE(A3:A, "[^\s]", ))+1, ))
    

    0