2ND Largest Word In A Cell

bsimecek

New Member
Joined
Oct 1, 2004
Messages
12
Is there a way to modify this VBA code to show the second largest word in a cell value?

Function LongestWord(ByVal str As String)
' Returns the longest word in a string of words
Dim x As Variant
Dim i As Long
str = Application.Trim(str)
x = Split(str, " ")
LongestWord = x(0)
For i = 1 To UBound(x)
If Len(x(i)) > Len(LongestWord) Then
LongestWord = x(i)
End If
Next i
End Function

http://www.mrexcel.com/board2/viewtopic.php?p=528592#528592
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi there,

You can try to keep the function you have got, and insert the following:

Sub Second_LongestWord()
Dim Second_LongestWord As String
tString = "This is a Test String"
Second_LongestWord = LongestWord(Replace(tString, LongestWord(tString), ""))
End Sub
 
Upvote 0
It looks like I can use:

Function Second_LongestWord(str) As String
'Dim Second_LongestWord As String
Second_LongestWord = LongestWord(Replace(str, LongestWord(str), ""))
End Function

Function LongestWord(str) As String
' Returns the longest word in a string of words
Dim x As Variant
Dim i As Long
str = Application.Trim(str)
x = Split(str, " ")
LongestWord = x(0)
For i = 1 To UBound(x)
If Len(x(i)) >= Len(LongestWord) Then
LongestWord = x(i)
End If
Next i
End Function

If Agresti & Agresti is the cell in question, "Agresti" is returned as the longestWord and "&" is returned as the secondLongestWord. Is there a way to fix this?
 
Upvote 0

Forum statistics

Threads
1,217,414
Messages
6,136,496
Members
450,016
Latest member
murarj

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