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.

Top N Products and Top N Sellers in Google Sheets (Top N of Top N)

If you’ve ever wanted to compare which sellers contribute the most to your top-selling...

Use XLOOKUP in a Structured Table in Google Sheets (Single and Multiple Conditions)

This tutorial is for users who want to apply XLOOKUP inside a structured table...

Reset SCAN by Another Column in Google Sheets and Excel

Resetting SCAN function results based on values in another column is a topic of...

How to Get the Fastest Time for Each Person in Google Sheets

Whether you’re tracking race results, event times, or any other timed activities, finding the...

More like this

Top N Products and Top N Sellers in Google Sheets (Top N of Top N)

If you’ve ever wanted to compare which sellers contribute the most to your top-selling...

Use XLOOKUP in a Structured Table in Google Sheets (Single and Multiple Conditions)

This tutorial is for users who want to apply XLOOKUP inside a structured table...

How to Get the Fastest Time for Each Person in Google Sheets

Whether you’re tracking race results, event times, or any other timed activities, finding the...

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.