Return longest string within cell

Mlex889

New Member
Joined
Nov 1, 2016
Messages
5
Hello Mr.Excel,

I need a way to return the longest string within a cell. (number of characters)
However, it should count strings based on line breaks within the cell.

For example,
==
This is an example
of text within one cell.
==

The above should return the value "24"

Thanks!!
Mike
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I used your example text and it worked fine with the example formula from the link I provided, first line is 20 characters, the second 24.
 
Upvote 0
I put
Code:
[COLOR=#333333]=LEN(MID($A1,SUMPRODUCT(N(OFFSET($A1,0,COLUMN($A1:A1)-COLUMN($A$1))))+1,FIND(CHAR(10),$A1&CHAR(10),SUMPRODUCT(N(OFFSET($A1,0,COLUMN($A1:A1)-COLUMN($A$1))))+1)-SUMPRODUCT(N(OFFSET($A1,0,COLUMN($A1:A1)-COLUMN($A$1))))))[/COLOR]
in B1 and your text in A1.
I then dragged the formula from B1 to C1, giving me 20 in B1 and 24 in C1.
 
Upvote 0
I put
Code:
[COLOR=#333333]=LEN(MID($A1,SUMPRODUCT(N(OFFSET($A1,0,COLUMN($A1:A1)-COLUMN($A$1))))+1,FIND(CHAR(10),$A1&CHAR(10),SUMPRODUCT(N(OFFSET($A1,0,COLUMN($A1:A1)-COLUMN($A$1))))+1)-SUMPRODUCT(N(OFFSET($A1,0,COLUMN($A1:A1)-COLUMN($A$1))))))[/COLOR]
in B1 and your text in A1.
I then dragged the formula from B1 to C1, giving me 20 in B1 and 24 in C1.


HI RedBeard,
I am putting the text in column A, and will put the formula in column B. The formula you provided does not display the number of characters in the longest string in A1 in B1. It seems to only display the number of characters in the first line of the cell.

Mike
 
Upvote 0
Hi,

You can do this with a single array formula** if you want:

=MAX(LEN(TRIM(MID(SUBSTITUTE(A1,CHAR(10),REPT(" ",LEN(A1))),1+LEN(A1)*(ROW(INDEX(A:A,1):INDEX(A:A,1+LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),""))))-1),LEN(A1)))))

Regards


**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).
 
Upvote 0
Hi,

You can do this with a single array formula** if you want:

=MAX(LEN(TRIM(MID(SUBSTITUTE(A1,CHAR(10),REPT(" ",LEN(A1))),1+LEN(A1)*(ROW(INDEX(A:A,1):INDEX(A:A,1+LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),""))))-1),LEN(A1)))))

Regards


**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

The new part in the second INDEX expression shortens the processing time by some tens of percentages compared to the usual INDEX(A:A,LEN(A1)) solution.
 
Last edited:
Upvote 0
The new part in the second INDEX expression shortens the processing time by some tens of percentages compared to the usual INDEX(A:A,LEN(A1)) solution.

Thanks, István. I recall now that you are something of an expert on the various forms of this construction!

Cheers
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top