Select next word in a cell

D

Dave Neve

Hi

How do you select the next word in a cell?

I want the cursor to move right until it comes to the next letter and then
select that word.

Thanks in advance

Dave
 
H

Helmut Weber

Hi Dave,
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.Words(1).Select
But to prevent the selection from selecting all of the cell,
which would happen, if it is on the last word,
not to mention what happens if it were in an empty cell,
would be quite another story.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
D

Doug Robbins

The following code will select the word in the cell that follows the word in
which the selection is currently located:

Dim i As Long, j As Long, pararange As Range
For i = 1 To Selection.Cells(1).Range.Words.Count
Set myrange = Selection.Cells(1).Range.Words(i)
If i < Selection.Cells(1).Range.Words.Count - 1 And InStr(myrange,
Selection) > 0 Then
Selection.Cells(1).Range.Words(i + 1).Select
Exit For
End If
Next i


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
D

Dave Neve

Hi

Thanks to both of you for coming to my aid again.
Helmet was right in that his macro presented me with certain problems for
what I want to do.

Dougs macro seems to work fine except that when applied to the last word in
a cell, it seems to select the word before.

I am really sorry but I can't figure out how to stop this in view of

Selection.Cells(1).Range.Words(i + 1).Select.

Why does i + 1 become i - 1 if it is the last word.

Thanks in advance
 
H

Helmut Weber

Hi Dave,
just for fun, and I wonder what your test results will show,
a much less smart approach than Doug's, kind of brute force:
Sub test()
' wdEndOfRangeColumnNumber = 17
' wdEndOfRangeRowNumber = 14
Dim r As Range
Dim iCll As Integer ' cell
Dim iRow As Integer ' row
ResetSearch
With Selection
If .Range.Text = .Cells(1).Range.Text Then
MsgBox "all of cell selected"
Exit Sub
End If
Set r = .Range
.Collapse direction:=wdCollapseEnd
iCll = .Information(17)
iRow = .Information(14)
With .Find
.Text = "<*>"
.MatchWildcards = True
.Execute
End With
If iCll <> .Information(17) Or iRow <> .Information(14) Then
r.Select
End If
End With
ResetSearch
End Sub
'---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
Have a nice day, both of you!
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
D

Doug Robbins

The following modified macro overcomes that problem:

Dim i As Long, myrange As Range
Selection.Words(1).Select
For i = 1 To Selection.Cells(1).Range.Words.Count
Set myrange = Selection.Cells(1).Range.Words(i)
If i < Selection.Cells(1).Range.Words.Count - 1 And InStr(myrange,
Selection) > 0 Then
Selection.Cells(1).Range.Words(i + 1).Select
Exit For
End If
Next i


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
D

Dave Neve

You have a strange idea of 'fun' Helmet but thanks anyway.

You too have a nice week.

Dave
 
D

Dave Neve

Hi Doug

Unless I am going barmy, this macro still displays the same problem.

Any chance of another look at it?

Thanks

Dave
 
D

Doug Robbins

Hi Dave,

I just tried it again and it seems to work OK whether there is nothing, a
space or a carriage return after the last word in the cell.

What do you actually have in the cell?

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
D

Dave Neve

Hi

You're gonna think I am mad but the words in the cell make a difference to
the behaviour of the macro.

If I type in the same word five times and execute the macro, it blocks on
the second word!!!

If the words are different, it seems to execute except on some cells where I
still get strange behaviour but can't work out why.

I suppose that as Word has a spell check which doesn't like repeated words,
sth in Word stops the macro from running on certain occasions.

But on the whole the macro works thanks.

Dave
 
D

Doug Robbins

OK, I've just reproduced that behavior (with the same word repeated). It's
bizarre.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
H

Helmut Weber

Hi Dave and Doug,
the macro in discussion cannot get over a repeated word.
Lets say the third word was selected, and the same word is before it,
then, if the word(i) is in the selection, the word after word(i)
would be selected, which could be the very selection itself,
or, even over a distance, the word after word(i) again.
So the selection would be back, were it was already once before.
Nice little riddle. ;-)
 
D

Doug Robbins

Hi Dave,

Fellow MVP, Jean-Guy Marcil, came up with the following which does the
trick.

Sub SelectNextWordinCell()

Dim CellEnd As Long
Dim WordEnd As Long
Dim WordRge As Range

With Selection
CellEnd = .Cells(1).Range.End
WordEnd = .Words(1).End

If WordEnd < CellEnd - 1 Then
Set WordRge = .Words(1)
Set WordRge = WordRge.Next(wdWord, 1)
WordRge.Select
Else
MsgBox "This is the last word in the cell."
End If
End With

End Sub


--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top