![]() |
Combining 2 tables into 1 and use Table2's column widths (hoping for workaround dealing merged cells
Hello,
I am trying to create a Word macro that will adjust every table in a document to have specific column widths. These documents I work with can vary anywhere from 15 pages to 1000 pages with varying table lengths, but always with 4 columns. The problem is there are merged cells (both vertical and horizontal) in each table and they are needed for this project, so they have to stay. I have been searching for days, but I have not been able to find a code that truly works around this issue. Since it looks like VBA can't modify a table with merged cells, I thought it might be possible to make a new table the way I want it formatted and then combine that with a table in the document. Is it possible to create a macro that can perform the following actions and if so, how would it look in VBA: 1) Find table (Table1) 2) Add new 1x4 table (Table2) above (or below if it matters) Table1 with desired column widths ' this would basically be a copy header row found on every table in document 3) Combine (snap together) Table1 and Table2, using Table2's column widths ' if not possible, then perhaps Table2's "table width" will work 4) Delete 2nd row (formerly 1st row) in each table 5) Next table Any advice would be appreciated! |
Working with horizontally merged cells means you'd probably have to loop through all cells and explicitly set their widths, one cell at a time. That also means you'll need to test each cell's width to work out whether it's too wide to fit a single column and, if so, how many columns it spans - simply testing the column index of the next cell on the row won't work, since that's always one more than the current one. This isn't too hard if the existing tables have a consistent layout but, if the column widths are all over the place (especially if the same nominal column has more than one width in an existing table), your code will also need to do some testing to work out what the current nominal column widths are - and that's made all the more difficult if auto-resizing is on.
|
1 Attachment(s)
Thanks for your reply! The tables I am working with should follow a set criteria, but they are often slightly different and sometimes drastically different (unfortunately). The standard layout contains 3 rows:
Row 1 = repeating title row Row 2 = column 1 vertically merged with row 3; criteria in columns 2-4 Row 3 = column 1 vertically merged with row 2; column 2-4 horizontally merged I'm attaching an example for a visual. Below are some attempts I've made. Note that they're not all complete and I know they don't work with merged cells. Do any of these attempts look promising for what I am trying to do? PHP Code:
|
The table image in your post depicts a 5-row * 4-column table. By how much do the non-standard tables differ from that table's structure?
|
My apologies; I didn't explain that very well. Each table will have a title row followed by any number of additional rows, but they will always follow that design where there are 4 cells in one row and then a merged cell underneath (this is the "standard" part I was referring to). The image I attached is showing 2 occurrences.
|
OK but, after the title row, do they all have the same structure (i.e. a pair of rows consisting of a merged cell spanning two rows on the left and the lower row of the pair merged across cells 2-4)?
|
Yes, that is correct.
|
Try the following macro for creating the new tables. They'll appear after the existing ones.
Code:
Sub Demo() |
Wow that is impressive! I take it there's no easier way than to create new tables, correct? If that is the case, is there a way to copy or cut the content from the original table, paste into the new table, and then delete the original?
|
Quote:
Quote:
|
Quote:
I tried your code yesterday and it worked great on the tables, but it seems to paste over 5-6 lines of text inbetween tables. I am still trying to figure out how your code works, but I'm wondering if this issue has something to do with the following part: Code:
With .Range.Paragraphs.Last.Next.Range In another part, removing an indent/tab on a line under a table removed 2 more lines than before. I am still playing around with other causes, but hopefully you know more about this cause than I do. |
Quote:
Quote:
|
1 Attachment(s)
That is good to hear! I think a sample might help so I put one together and attached it here. Hopefully it provides what you need.
|
Try:
Code:
Sub Demo() |
That just about did it! Would you be willing to explain what this code is doing so I know where I went wrong? Also, I'm experiencing some minor issues when I run this code:
- The tables as a whole are left aligned, then some are halfway off the left side. How could you make them centered in the page? - When I try to add code to repeat the first row, I get an error again about merged cells. Is it a matter of where it is placed in the code or how? I've been trying this code: Code:
Tbl.Rows(1).HeadingFormat = True Code:
If Case 1: .Cells.Merge = False Then Code:
If .Cells().Width = InchesToPoints(6.21) Then |
All times are GMT -7. The time now is 07:16 PM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by
DragonByte SEO (Lite) -
vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft