Find longest word in cell

BobMango

New Member
Joined
Sep 9, 2009
Messages
2
Anyone have a quick way to find the length of the longest word within a cell? I want to use a macro to adjust my column widths based on this value.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
=LEN(A1) would display the number of characters in cell A1
 
Upvote 0
Hi Bob, hopefully this gives you an idea of how to do it:
Code:
Sub BobMango()
Dim myArr, i As Long, bigWord As String
 
[COLOR=green]' Split activecell's value into an array using spaces as the delimiter[/COLOR]
myArr = Split(ActiveCell.Value, " ")
bigWord = ""
 
[COLOR=green]' Loop through the array and test if the current word is larger than[/COLOR]
[COLOR=green]' bigWord (initialized as blank). If so, bigWord is set to the[/COLOR]
[COLOR=green]' current word.[/COLOR]
For i = 0 To UBound(myArr)
    If Len(myArr(i)) > Len(bigWord) Then
        bigWord = myArr(i)
    End If
Next i
 
[COLOR=green]' Display the largest word in the cell via message box[/COLOR]
MsgBox bigWord
 
End Sub
 
Upvote 0
Thanks mvptomlinson! I think that will do the trick. I just need to lay that into a loop for all cells in a column and we'll be home free.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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