Find the Longest String in Each Row in Google Sheets

Published on

You can use the following formula to find the longest string in a row or the longest string in each row in Google Sheets:

=BYROW(range, LAMBDA(row, TEXTJOIN(", ", TRUE, FILTER(row, LEN(row)=MAX(LEN(row))))))

In this formula, replace range with the actual range you want to apply it to.

Example of Finding the Longest String in a Row

Assume you want to find the longest string in the range B2:F2.

Insert the following formula in G2:

=BYROW(B2:F2, LAMBDA(row, TEXTJOIN(", ", TRUE, FILTER(row, LEN(row)=MAX(LEN(row))))))
Example of Finding the Longest String in Each Row in Google Sheets
screenshot#1

Example of Finding the Longest String in Each Row

If you want to find the longest string in each row in the range B2:F6, use this formula:

=BYROW(B2:F6, LAMBDA(row, TEXTJOIN(", ", TRUE, FILTER(row, LEN(row)=MAX(LEN(row))))))

This will return the longest string(s) from each row separately.

Formula Explanation

Let’s break down the concept behind the formula.

Start with:

=FILTER(B2:F2, LEN(B2:F2)=MAX(LEN(B2:F2)))

Here:

  • FILTER picks the values in B2:F2 where the length matches the maximum length in that row.
  • If there are multiple strings tied for the longest length, it will return all of them.

To join tied values into a single text string, we wrap it with TEXTJOIN:

=TEXTJOIN(", ", TRUE, FILTER(B2:F2, LEN(B2:F2)=MAX(LEN(B2:F2))))

The TEXTJOIN function combines the tied values into one string, separated by commas.

Now, to make this reusable, we convert it into a custom LAMBDA function:

LAMBDA(row, TEXTJOIN(", ", TRUE, FILTER(row, LEN(row)=MAX(LEN(row)))))

Here, row acts as a placeholder for any row you apply it to.

Finally, we use BYROW to apply this formula across a row or each row:

=BYROW(array_or_range, lambda)

Replace array_or_range with B2:F2 (for a single row) or B2:F6 (for multiple rows), and use the custom lambda function we just created.

That’s your formula to find the longest string in a row or the longest string in each row in Google Sheets.

FAQs

How do I find the First String with the Longest Length in a Row?

If you want only the first longest string instead of all tied ones, modify the LAMBDA function like this:

LAMBDA(row, INDEX(row, XMATCH(MAX(LEN(row)), LEN(row))))

Example:

=BYROW(B2:F6, LAMBDA(row, INDEX(row, XMATCH(MAX(LEN(row)), LEN(row)))))

Does this Work with Numbers?

Yes! This approach works with numbers as well, based on the character length of the number (not its numeric value).

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Expert Prashanth KV is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

Mode of Comma-Separated Numbers in Excel (Dynamic Array)

There is no dedicated function in Excel to directly find the mode of comma-separated...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

More like this

Calculate Trip Days by Month (Start, End, and Full Days) in Google Sheets

If you're managing business travel in Google Sheets, you may need to calculate how...

How to Find Mode of Comma-Separated Numbers in Google Sheets

Working with comma-separated numbers inside a single cell is a common scenario in Google...

How to Count Merged Cells in Google Sheets (and Get Their Size)

Sometimes, you may have blocks of merged cells in a column or row. But...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.