How To Select a Row Within a Nested Table Programatically?

B

bhartman33

Hi, Everyone.

I'm dealing with a table, which has a nested table in each row of
column 9. What I would like to do is to take a specifc row of this
nested table, and copy that row into column 10. My problem is I can't
get the code right to refer to a specific row within this nested table.
Here's my code:

Dim mtable As Range
Set mtable = ActiveDocument.Tables(2).Range
mtable.Rows(5).Cells(9).Select

My problem is that this code selects the entire nested table, rather
than a row within the nested table. Can anyone set me straight?

Thanks!
 
H

Helmut Weber

Hi,

how about this one, in priciple:

Sub test81045()
With ActiveDocument.Tables(1)
.Cell(1, 2).Select
Selection.End = Selection.End - 1
Selection.Tables(1).Select
End With
End Sub

Or, using ranges:

Sub test81046()
Dim rTmp As Range
With ActiveDocument.Tables(1)
Set rTmp = .Cell(1, 2).Range
rTmp.End = rTmp.End - 1
Set rTmp = rTmp.Tables(1).Rows(2).Range
MsgBox rTmp
End With
End Sub


From then on it's plain sailing, IMHO

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
G

Greg

Seems to me that you don't need to select anything.

Try something along these lines:
Sub Test()
Dim oCell As Cell
Dim oTbl As Table
Dim i As Long
Set oTbl = ActiveDocument.Tables(1)
With oTbl
For i = 1 To .Rows.Count
Set oCell = .Cell(i, 9)
With oCell.Tables(1)
oTbl.Cell(i, 10).Range.Text = .Rows(2).Range.Text
End With
Next
End With
End Sub

This makes the text in column 10 of the parent table match the text of
row 2 of the first nested table in column 9 of the parent table. There
is no selection, copying or pasting required.
 
B

bhartman33

Hi, Greg, Helmut.

I should have specified what I'm doing with the table first. What I
need to do is have a macro that searches column 9 for a specific term,
then selects the entire row when it finds that term, then copies that
row into column 10. That's why I thought selection was necessary. I'm
not at the right computer right now to try those macros, but I will try
them tonight and let you know how it goes. The help from this forum
saved my bacon with the last part of this project, so I know I can work
through this part with your help! :)
 
G

Greg Maxey

Ahhh, I still don't think you need to select.

Try:

Sub Test()
Dim oCell As Cell
Dim oTbl As Table
Dim oRow As Row
Dim i As Long
Set oTbl = ActiveDocument.Tables(1)
With oTbl
For i = 1 To .Rows.Count
Set oCell = .Cell(i, 9)
For Each oRow In oCell.Tables(1).Rows
With oRow.Range.Find
.Text = "Yourterm"
.Execute
If .Found = True Then
oTbl.Cell(i, 10).Range.Text = oRow.Range.Text
End If
End With
Next
Next
End With
End Sub
 
B

bhartman33

Hi, Greg.

That macro didn't work for me. It's telling me that the collection
doesn't exist when it hits "For Each oRow In oCell.Tables(1).Rows".

I haven't tried Helmut's solutions yet. (My wireless adapter died on
my laptop, so I wasn't able to mail myself the macros.) I will get
back to work on this tomorrow. I'm much more confident now that a
solution can be found, though. :)
 
G

Greg Maxey

I tested with a single 10x10 table. Column 9 has one 1x3 table. The problem
might be the line

Set oTbl = ActiveDocument.Tables(1).

That means the first table in the document. If your first table has at
least 9 columns but doesn't have a nested table in column 9 then that error
would occur.


So
Try
Set oTbl = ActiveDocument.Tables("the index number of your table")
 
B

bhartman33

Hi, Greg.

I think the problem is the header row. That row itself has a nested
table, and the first row of that nested table is only one column. I'll
change the macro to run from the second row and see what happens.
Thanks!
 

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