Dividing up Tables in Word

J

Joe

This is my third time to ask for help on this problem in the last 2
months. It must be a difficult code to write, but maybe someone has an
idea.
I'd like a macro that will break a table up into smaller tables,
duplicating column one and pairing it with each subsequent column.
The tables are of 3 to 5 columns and
a 8 to 20 rows. In each table, the first column always contains labels.

For example:
Record1 Record2
Label1 4 2
Label2 5 4
Label3 Blue Red

I'd like to get a macro that goes through the current document and
breaks up every table with more than 2 columns. Ideally, it would
remove each column beyond column 2 and attach it to the labels column
to be inserted as sepeate tables following the original.

For example:
Record1
Label1 4
Label2 5
Label3 Blue

Record2
Label1 2
Label2 4
Label3 Red

Can anyone show me and example of such a macro.

Thanks,
Joe
 
J

Joe

I will write the macro that selects every table in the document that's
over 2 columns. Something like:

Dim tbl as Word.Table
For each tbl in ActiveDocument.Tables
If tbl.Columns.Count > 2 Then
tbl.Select
' ?????????
End If
Next tbl

But what I don't know how to do is the part at ??????????? that needs
to replace the 3 or more columned tables into a series of unjoined 2
column tables.
The use is a series of files that contain these various tables, some 60
per document.

Thanks for any help,
Joe
 
M

Malcolm Smith

Joe

I wouldn't bother selecting the table; just continue using the Table
pointer, in this case tbl, as this will save no end of grief.

What I would do is as follows.

Create an array of strings which is two dimensional which mirrors out the
table, including the headings and the row titles.

Then I would delete all the rows, save one of the table and then all the
columns save two.

Next I would expand the number of rows in the table by the required
number. Simple maths would get the amount of rows required.

Next go through the array and start throwing the data into the table and
then remembering to move onto the next cell with the wdCellNext, or
whatever it is, argument and then you have the thing done.

Hope that this helps
- Malc
www.dragondrop.com
 
J

Joe

VBA is not my primary language, and I'm not familiar enough right now
to know how best to store all the data from the table. In other words,
I know exactly what your saying, just lack the ability to punch out the
code.
I guess that's why I was thinking more in terms of tbl.select and then
cutting and pasting columns.
 
D

Doug Robbins - Word MVP

Hi Joe,

I think that this does what you want:

Dim i As Integer, j As Integer
ActiveDocument.Tables(1).Range.Copy
For i = 1 To ActiveDocument.Tables(1).Columns.Count - 2
Selection.EndKey wdStory
Selection.TypeText vbCr
Selection.Paste
Next i
For j = ActiveDocument.Tables(1).Columns.Count To 3 Step -1
ActiveDocument.Tables(1).Columns.Last.Delete
Next j
For i = 2 To ActiveDocument.Tables.Count
For j = i To 2 Step -1
ActiveDocument.Tables(i).Columns(j).Delete
Next j
For j = ActiveDocument.Tables(i).Columns.Count To 3 Step -1
ActiveDocument.Tables(i).Columns.Last.Delete
Next j
Next i

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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