Quick, clean, and to the point

Find longest string with criteria

Excel formula: Find longest string with criteria
Generic formula 

To find the longest string in a range with criteria, you can use an array formula based on INDEX, MATCH, LEN and MAX. In the example shown, the formula in F6 is:


Where "names" is the named range C5:C14, and "class" is the named range B5:B14.

Note: this is an array formula and must be entered with control + shift + enter.


The core of this formula is the MATCH function, which locates the position of the longest string using supplied criteria:


Note MATCH is set up to perform an exact match by supplying zero for match type. For lookup value, we have:


The LEN function returns an array of results (lengths), one for each name in the list where class = "A" from cell F5 :


This effectively filters out all of Class B, and the MAX function then returns the largest value, 8.

To construct a lookup array, we use the same approach:


And get the same result:


After LEN and MAX run, we have a MATCH formula with these values:


MATCH then returns the position of 8 in the list, 3, which feeds into INDEX like this:


Finally, INDEX dutifully returns the value in the 3rd position of names, which is "Jonathan".

Dave Bruns

Excel Formula Training

Formulas are the key to getting things done in Excel. In this accelerated training, you'll learn how to use formulas to manipulate text, work with dates and times, lookup values with VLOOKUP and INDEX & MATCH, count and sum with criteria, dynamically rank values, and create dynamic ranges. You'll also learn how to troubleshoot, trace errors, and fix problems. Instant access. See details here.

Download 100+ Important Excel Functions

Get over 100 Excel Functions you should know in one handy PDF.