How to get and copy values from one table to second table?

A

avkokin

Hello.
There is one table (table1) which has 5 columns and different number
rows. First 3 columns has some values. I'm create new table (table2)
below with 7 columns. I need to copy values from first 3 columns of
table1 to columns of table2. I am repeating: previously number of rows
from table1 maybe is unknown .
Question: how to get and copy values from one table to second table?
For example I did one document - http://www.box.net/shared/y0taiiecko.
Thank you very much.
 
S

StevenM

SubTo: Avkokin,

It all depends on how much checking you feel is necessary. But the following
code should give you the basic idea.

Sub CopyTable1ToTable2()
Dim fmTable As Table
Dim toTable As Table
Dim i As Long
Dim j As Long
Dim s As String

If ActiveDocument.Tables.Count > 2 Then
Set fmTable = ActiveDocument.Tables(1)
Set toTable = ActiveDocument.Tables(2)

If fmTable.rows.Count >= 5 _
And toTable.rows.Count >= 5 Then
For i = 3 To 5
If fmTable.rows(i).Cells.Count >= 2 _
And toTable.rows(i).Cells.Count >= 2 Then
For j = 1 To 2
s = fmTable.rows(i).Cells(j).Range.Text
s = Left(s, Len(s) - 2)
toTable.rows(i).Cells(j).Range.Delete
toTable.rows(i).Cells(j).Range.Text = s
Next j
End If
Next i
End If
End If
End


You can run this code on: http://www.box.net/shared/y0taiiecko

Steven Craig Miller
 
A

avkokin

SubTo: Avkokin,

It all depends on how much checking you feel is necessary. But the following
code should give you the basic idea.

Sub CopyTable1ToTable2()
Dim fmTable As Table
Dim toTable As Table
Dim i As Long
Dim j As Long
Dim s As String

If ActiveDocument.Tables.Count > 2 Then
Set fmTable = ActiveDocument.Tables(1)
Set toTable = ActiveDocument.Tables(2)

If fmTable.rows.Count >= 5 _
And toTable.rows.Count >= 5 Then
For i = 3 To 5
If fmTable.rows(i).Cells.Count >= 2 _
And toTable.rows(i).Cells.Count >= 2 Then
For j = 1 To 2
s = fmTable.rows(i).Cells(j).Range.Text
s = Left(s, Len(s) - 2)
toTable.rows(i).Cells(j).Range.Delete
toTable.rows(i).Cells(j).Range.Text = s
Next j
End If
Next i
End If
End If
End

You can run this code on:http://www.box.net/shared/y0taiiecko

Steven Craig Miller

Hello Steven.
Thank you! I get new problem with the table if it has merged cells. I
will open new post.
Sincerely, Anton Kokin
 

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