Excel Longest Word of Cell Formula

bsimecek

New Member
Joined
Oct 1, 2004
Messages
12
I am trying to find a formula that will return the longest word in a cell. Each cell in question has a sentance like structure. I have found VBA code to do this, but I'm trying to find a formula.

Here is the VBA code:

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

Anyone have any Ideas?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Why not use the User Defined Function you provided? Any formula, provided that one can be written, will probably be very long and complicated.
 
Upvote 0
Formulas will be easier to implement and I will not have to worry about crossing versions as much.

Any ideas?
 
Upvote 0
google on how to make a macro a custom formula.. thad be the easiest way (if possible)
 
Upvote 0
I really think VBA is the answer.

eg How will you work out with a formula how many words there are?

The only problem I can see about version crossing is the use of the Split function, which AFAIK is only available in later versions.

You could overcome that writing your own Split function.
 
Upvote 0
Actually, UDFs are pretty easy to implement. All you need to do is cut and paste the code in the VB editor, then use it like any other formula, i.e.:

=LongestWord("A1")
 
Upvote 0
I don't know that much about VBA, I understand the use of forumuals better. If I have a column in excel that needs to be processed, how would i use the vba for each record?
 
Upvote 0
I recognize this line, but when used in excel 2003, I just get an #NAME? Error. Is there somthing that I need to set up seperately?
 
Upvote 0
You really don't need to know much of anything about VBA to use this code.

Simply go to Tools | Macro | Record New Macro and click OK. Then hit the Stop Button immediately. Now go to Tools | Macro | Macros, select the one macro (probably named Macro1) and click Edit. A window woll pop-up with VBA code. Simply cut and paste the code provided over top of what is already in there, save, and exit the VB editor.

You have now created the user defined fucntion that you can use like I specified in my last post.
 
Upvote 0

Forum statistics

Threads
1,217,415
Messages
6,136,503
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