HomeGoogle DocsSpreadsheetFind the Longest String in a Column in Google Sheets

Find the Longest String in a Column in Google Sheets

Published on

To find the longest string in a column in Google Sheets, we can use Index-Match or Filter-based formulas.

Both the formulas have their plus and minuses. I will explain that later. First of all, let’s try to understand the scenario.

It’s all about finding strings based on the maximum number of characters.

Assume I have a list of names in the range B2:B in Google Sheets.

I want to return the name that has the maximum number of characters.

Two Formulas to Find the Longest String in a Column in Google Sheets

We will start with Index-Match in combination with Max and Len.

Option 1

Apply the following formula in cell C2.

=index(
     B2:B,
     match(max(len(B2:B)),len(B2:B),0),
     0
)
Return the Longest String in a Column - Google Sheets Formula

The above index-match formula returns the longest string in column range B2:B in cell C2, which is “Ernestine.”

Can you explain it?

Why not? Here you go!

Syntax:-

INDEX(
     reference,
     row,
     column
)

Reference – B2:B

Rowmatch(max(len(B2:B)),len(B2:B),0)

Column – 0

The above Match formula (Row) plays a vital role in finding the longest string in column B.

It returns the relative position of the longest string in cell range B2:B.

The Index offsets that many numbers of rows and zero columns.

As per the above example, the match returns the number 10, i.e., the relative position of the longest string in B2:B.

So the Index offsets ten rows and zero columns.

Let’s try to understand the Match formula. Here it is!

Syntax:-

MATCH(search_key, range, search_type)

Search Keymax(len(B2:B))

Rangelen(B2:B)

Search Type – 0

If you could understand len(B2:B) part, you can easily understand the formula. Here it is!

Note:- Within Index, we can use len(B2:B), but in standalone use, you should use it as arrayformula(len(B2:B)).

The Role of LEN in Index-Match

The above (column C) is the Match formula range (the LEN returns the length of characters in each name).

The search key is the MAX value in this range (column C) and which will be 9 (please see the Cyan highlighted cells).

So the Match matches the character length 9 (search key) and returns 10, which is the relative position of the search key in the range.

Option 2

Here is one more formula that you can use to find the longest string in a column in Google Sheets.

=filter(B2:B,len(B2:B)=max(len(B2:B)))

It is much cleaner than the OPTION 1 formula above.

Here, the formula filters the names based on the length of strings. It’s like this.

If the length of the strings is equal to its max length in the range, return that value.

This formula sometimes returns more than one value.

For example, if two names have nine characters each, the formula will return both.

But the Index-match will return the first name only.

Array Formula to Find the Longest String in Each Column

If you want to perform the same evaluation in each column, you can better depend on an array formula alternative to index-match.

In the below example, the formula in cell B8 finds the longest values in each column in the range B2:F6.

=ArrayFormula(
     vlookup(
        dmax(
           {column(B1:F1);len(B2:F6)},sequence(1,columns(B1:F1),1),
           {column(B1);
           if(,,)}
        ),
        H2:I5,
        2,0
    )
)
Finding the Longest String in Each Column in Google Sheets

Formula Explanation

It’s a Vlookup array formula.

Syntax:

ARRAYFORMULA(VLOOKUP(search_key, range, index, [is_sorted]))

Here is the range reference in that formula.

Range

See the helper range H2:I5. It acts as the range in Vlookup.

Cell range I2:I5 contains all the unique strings in B2:F6.

You can manually enter that or use the below formula in I2.

=sort(filter(unique(flatten(B2:F6)),unique(flatten(B2:F6))<>""))

In H2:H5, I have entered the length of the above unique strings.

Here I have used the below formula in cell H2.

=ArrayFormula(len(I2:I5))

Now back to the array formula in cell B8 that returns the longest string in each column.

We have already seen the range used in this formula above. The search keys are the DMAX formula which is as follows.

Search Key

dmax({column(B1:F1);len(B2:F6)},sequence(1,columns(B1:F1),1),{column(B1);if(,,)})

Let’s test it. For that, we should wrap it with the ARRAYFORMULA function as below.

=ArrayFormula(dmax({column(B1:F1);len(B2:F6)},sequence(1,columns(B1:F1),1),{column(B1);if(,,)}))
The Search Keys Returned by LEN

The above DMAX returns the length of the largest string in each column.

Vlookup uses it as the search key to search the first column in the range H2:I5 and returns the corresponding values from I2:I5.

DMAX (Search Key) Explanation

Syntax:-

DMAX(database, field, criteria)

Database{column(B1:F1);len(B2:F6)}

As you may know, the database functions require field labels.

Since our above range B2:F6 doesn’t contain the same, I have used column numbers as the field labels.

Let’s test the database (we should use ArrayFormula with the database in standalone test).

The Role of DMAX to Return the Longest String in Every Column

Fieldsequence(1,columns(B1:F1),1)

It’s equal to specifying ={1,2,3,4,5}, i.e., the column indexes.

Criteria{column(B1);if(,,)}

The first field label is 2 (please see cell B8 the image above), and =column(B1) returns that.

The IF returns a null character that means no criteria.

That’s all about how to find the longest string in each column in Google Sheets.

Thanks for the stay. Enjoy!

Similar: Find the Longest String in Each Row in Google Sheets.

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.

SORT and SORTBY – Excel Vs Google Sheets

While Excel offers the SORT and SORTBY functions for sorting a table, Google Sheets...

Get Top N Values Using Excel’s FILTER Function

When using the FILTER function in Excel to extract the top N values, it's...

XLOOKUP in Excel: Working with Visible Rows in a Table

In Excel, the XLOOKUP function isn't inherently designed to exclusively work with visible rows;...

SUMPRODUCT Differences: Excel vs. Google Sheets

The core purpose of the SUMPRODUCT function remains consistent across both Excel and Google...

More like this

Interactive Random Task Assigner in Google Sheets

You have multiple tasks and multiple people. Here's a fun way to randomly assign...

Google Sheets Bar and Column Chart with Target Coloring

To color each data point in the column or bar chart according to the...

Analyzing Column A Between Non-Adjacent Values in Column B: Google Sheets

This tutorial addresses a common scenario encountered in Google Sheets: how to sum, average,...

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.