Looking for a formula to get biggest word between the underscores

sandhya16

New Member
Joined
Jun 8, 2015
Messages
34
I have data like -

shi007yyyy_1_2
ggfdf_dsdjskhd777_v_2
hdjshdjs_1_kkkkkkkkds


I am looking for a formula to get the biggest word out of the data-

So the answer i am looking for should be -
shi007yyyy
dsdjskhd777
kkkkkkkkds

Left,Right & mid works but i have lots of data and i cannot change the formula each time.
Please help.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
UPDATE - Also if there is no underscore in the string then return the original string-

shi007yyyy_1_2
ggfdf_dsdjskhd777_v_2
hdjshdjs_1_kkkkkkkkds
hdsiioooo

So the answer i am looking for should be -
shi007yyyy
dsdjskhd777
kkkkkkkkds
hdsiioooo


 
Upvote 0
Hi,

Can you simply use Text to Columns (under the Data tab) select "Delimited" not "fixed width" and then select "other" and put an underscore in the box.

Then after 4 or so columns, whichever is free of data, you can fill an equal number of columns with len(A1), len(B1)... etc.

Finally in the next free column you can use '=INDEX(A1:D1,MATCH(MAX(F1:I1),F1:I1,0))

That should work as a quick fix.
 
Upvote 0
If there are 9 or fewer _ in the string, you could use

=MAX(FIND("~",SUBSTITUTE("_"&A1&"_","_","~",{2;3;4;5;6;7;8;9;10})&"~")-FIND("~",SUBSTITUTE("_"&A1&"_","_","~",{1;2;3;4;5;6;7;8;9})&"~"))-1
 
Upvote 0
Try this UDF:

Code:
Function Getlong(s As String) As String
s = "_" & s & "_"
x = Split(s, "_")
y = x(0)
Getlong = x(0)
For i = 1 To UBound(x) - 1
    If (Len(x(i)) > Len(y)) Then y = x(i)
Next
Getlong = y
End Function
 
Upvote 0
Mike has given you a nice formula. I couldn't resist, though, I think it can be shortened to...

=MAX(MMULT(FIND("~",SUBSTITUTE("_"&A1&"_","_","~",{2;3;4;5;6;7;8;9;10}-{0,1})&"~")*{1,-1},{1;1}))-1
 
Upvote 0
Mike has given you a nice formula. I couldn't resist, though, I think it can be shortened to...

=MAX(MMULT(FIND("~",SUBSTITUTE("_"&A1&"_","_","~",{2;3;4;5;6;7;8;9;10}-{0,1})&"~")*{1,-1},{1;1}))-1

I thought the OP needed the longest substring, not only its length.
 
Upvote 0
Try this UDF:

Code:
Function Getlong(s As String) As String
s = "_" & s & "_"
x = Split(s, "_")
y = x(0)
Getlong = x(0)
For i = 1 To UBound(x) - 1
    If (Len(x(i)) > Len(y)) Then y = x(i)
Next
Getlong = y
End Function
Your UDF can be write more compactly like this...
Code:
Function Getlong(s As String) As String
  For Each V In Split(s, "_")
    If Len(V) > Len(Getlong) Then Getlong = V
  Next
End Function
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

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