Offset in Word

J

joseph.choi13

How do I find a cell with the words "this cell", and get the value in
the next cell in word? Is there an offset function?

For Each oCell In oTableRge.Cells
If (InStr(1, Left((oCell.Range.Text),
Len(oCell.Range.Text) - 1), "this cell", vbTextCompare)) > 0 Then

**Get the value in the next cell**

End If
Next


Thanks in advance
 
D

David Sisson

How do I find a cell with the words "this cell", and get the value in
the next cell in word? Is there an offset function?

Dim MyText As Range
Dim oRow As Integer
Dim oCol As Integer

Set oTableRge = ActiveDocument.Tables(1).Range.Cells
For Each oCell In oTableRge
If (InStr(1, Left((oCell.Range.Text), _
Len(oCell.Range.Text) - 1), "This cell", vbTextCompare)) > 0
Then
oRow = oCell.Row.Index
oCol = oCell.Column.Index
Set MyText = ActiveDocument.Tables(1).Cell(oRow, oCol +
1).Range
MyText.MoveEnd wdCharacter, -1
MsgBox (MyText)
End If
Next
 
R

Russ

How do I find a cell with the words "this cell", and get the value in
Yes, there is 'sort of' an offset function.
With collections you can use the .first .last .next .previous properties or
method. See VBA help.
And chain them together like .first.next.next to point to the third one in
the collection. By default they may work on characters.
They seem to work for ranges that to point to a collection item as well, so
in the middle of the loop below you could use:
Set MyText = oCell.Range.next(wdCell)

And it would point to the next cell even if it was on the next row.
But you might add to the end of the if statement...
And Not oCell.Range.next(wdCell) is Nothing
....To avoid an error at end of cells.

Also, if you only need to find one instance of "this cell", then put an...
Exit For
....after processing the find, to stop looping through the rest of the
collection.
 

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