code to move cursor up two cells?

G

Gil Carter

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
 
J

Jay Freedman

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

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
 

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