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 Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

How to Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

Filter Items Unique to Groups in Google Sheets

In this tutorial, we'll learn how to filter items unique to groups in Google...

Find Common Items Across Multiple Columns in Google Sheets

This tutorial explains how to find common items across multiple columns in Google Sheets....

Sort Column by Length of Text in Google Sheets

To sort a column by length of text, you can either use the QUERY...

More like this

How to Sort Pie Slices in Google Sheets

To sort pie slices in a pie chart, you need to sort the data...

Filter Items Unique to Groups in Google Sheets

In this tutorial, we'll learn how to filter items unique to groups in Google...

Find Common Items Across Multiple Columns in Google Sheets

This tutorial explains how to find common items across multiple columns in Google Sheets....

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.