Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2005

Tip: Looking for answers? Try searching our database.

code to move cursor up two cells?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gil Carter - 13 Mar 2005 22:22 GMT
Hi All,
I seem to be wading around ... I am looking for code to move cursor up 2
whole cells in a table.  That's got to be the easiest thing in the world,
yes?

   Selection.MoveUp Unit:=wdCell , Count:=2, Extend:=wdMove

All help is greatfully appreciated. :)
Gil
Jay Freedman - 14 Mar 2005 02:50 GMT
>Hi All,
>I seem to be wading around ... I am looking for code to move cursor up 2
[quoted text clipped - 5 lines]
>All help is greatfully appreciated. :)
>Gil

Hi Gil,

You might think that should work, by analogy with other kinds of
moves. But the Help topic for the MoveUp method specifies which
constants work, and wdCell isn't one of them:

"Unit    Optional Variant. The unit by which to move the selection.
Can be one of the following WdUnits constants: wdLine, wdParagraph,
wdWindow or wdScreen."

There are a lot of complications to take into consideration -- what do
you want to do if the Selection is less than 2 rows from the top of
the table? What if it's partly in a table and partly outside it? Have
a look at this code, which handles some but not all possibilities (for
example, it doesn't explicitly check if the destination cell doesn't
exist because it was merged with one or more cells above it; that will
trigger the On Error trap).

Sub MoveUp2Cells()
   Dim oTbl As Table
   Dim nRow As Long, nCol As Long
   
   On Error GoTo Bye
   
   With Selection
       ' make sure selection isn't extended
       If .Type <> wdSelectionIP Then
           .Collapse wdCollapseStart
       End If
       
       ' make sure it's in a table
       If Not .Information(wdWithInTable) Then
           Exit Sub
       End If
       
       Set oTbl = .Tables(1)
       nRow = .Information(wdStartOfRangeRowNumber)
       nCol = .Information(wdStartOfRangeColumnNumber)
       
       ' make sure it can move up 2 rows
       If nRow < 3 Then
           GoTo Bye
       End If
       
       oTbl.Cell(nRow - 2, nCol).Select
       .Collapse wdCollapseStart
   End With
   
Bye:
   Set oTbl = Nothing
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP         FAQ: http://word.mvps.org
Gil Carter - 14 Mar 2005 04:20 GMT
Thanks Jay!,

Yours is a complete answer as seemingly always.  Thanks. :) :)
:)
Gil

>>Hi All,
>>I seem to be wading around ... I am looking for code to move cursor up 2
[quoted text clipped - 62 lines]
> Jay Freedman
> Microsoft Word MVP         FAQ: http://word.mvps.org 
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.