![]() |
|
![]() |
|
Thread Tools | Display Modes |
|
#1
|
||||
|
||||
![]() Try: Code:
Sub Demo() Application.ScreenUpdating = False Dim wdDoc As Document, Tbl As Table, sWdth(), i As Long, Rng As Range 'Store all the widths used. 1st item = table width '2nd - 5th items = individual cell widths '6th item = sum of of 3rd - 5th items sWdth = Array(6.66, 0.45, 1.5, 2.38, 2.33, 6.21) Set wdDoc = ActiveDocument With wdDoc For Each Tbl In .Tables With Tbl 'Give the cells an all-round interior padding .TopPadding = InchesToPoints(0.05) .BottomPadding = InchesToPoints(0.05) .LeftPadding = InchesToPoints(0.05) .RightPadding = InchesToPoints(0.05) .Spacing = 0 'Switch off autofit .AllowAutoFit = False 'set the preferred width type .PreferredWidthType = wdPreferredWidthPoints 'set the preferred width value to the specified array value .PreferredWidth = InchesToPoints(sWdth(0)) With .Rows 'Remove any table indenting .LeftIndent = 0 'Center the table on the page .Alignment = wdAlignRowCenter .RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin 'Give the cells automatic heights, so they'll fit their contents .HeightRule = wdRowHeightAuto End With With .Range 'Ensure the first row functions as a heading row .Cells(1).Select: Selection.Rows.HeadingFormat = True 'Format the first four cells For i = 1 To 4 .Cells(i).Width = InchesToPoints(sWdth(i)) Next 'Format the remaining cells, in groups of 5. 'The first cell in each group spans two rows and 'the first row in each group contains 4 cells but 'the second row contains 1 cell spanning the width 'of the last 3 cells of the first row For i = 5 To .Cells.Count 'If it's the first cell in a group, ensure it's both merged and centred vertically If ((i Mod 5) + 1) = 1 Then If .Cells(i + 4).ColumnIndex = 1 Then Set Rng = .Cells(i).Range Rng.End = .Cells(i + 4).Range.End Rng.Select: Selection.Cells.Merge End If .Cells(i).VerticalAlignment = wdCellAlignVerticalCenter End If 'Specify a minimum height for the last cell in the group If ((i Mod 5) + 1) = 5 Then .Cells(i).SetHeight Rowheight:=InchesToPoints(0.5), HeightRule:=wdRowHeightAtLeast End If .Cells(i).Width = InchesToPoints(sWdth((i Mod 5) + 1)) 'If we've merged any cells, the count will have changed If i = .Cells.Count Then Exit For Next End With End With Next End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] Last edited by macropod; 09-26-2015 at 02:18 AM. Reason: Code revision |
#2
|
|||
|
|||
![]()
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 .Cells.Merge Else Next Code:
If .Cells().Width = InchesToPoints(6.21) Then .Cells.Height = InchesToPoints(0.8) End If |
#3
|
||||
|
||||
![]() Quote:
Quote:
Quote:
Quote:
I've incorporated the above, plus comments into the code in my previous post.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
![]()
I'm getting an error with this section saying "The requested member of the collection does not exist." When I hover over the i, it says i = 10, if that helps at all.
Code:
If .Cells(i + 4).ColumnIndex = 1 Then Set Rng = .Cells(i).Range Rng.End = .Cells(i + 4).Range.End Rng.Select: Selection.Cells.Merge End If Code:
With Tbl .Range.Rows.WrapAroundText = False Code:
For i = 5 To .Cells.Count Select Case (i Mod 5) + 1 Case 1: sWdth = InchesToPoints(0.45) .Cells().VerticalAlignment = wdCellAlignVerticalTop Code:
With .Range .Cells(1).Select: Selection.Rows.HeadingFormat = True For i = 1 To 4 .Cells(i).Width = InchesToPoints(sWdth(i)) Next ''my addition If .Cells.Width = InchesToPoints(0.45) Then .Cells().VerticalAlignment = wdCellAlignVerticalTop End If |
#5
|
||||
|
||||
![]() Quote:
Quote:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
![]()
Today the same section is giving me a different error, this time regarding the Selection part:
Code:
If ((i Mod 5) + 1) = 1 Then If .Cells(i + 4).ColumnIndex = 1 Then Set Rng = .Cells(i).Range Rng.End = .Cells(i + 4).Range.End Rng.Select: Selection.Cells.Merge End If I got rid of text wrapping by adding the following code near the end, but that seemed to make the columns slightly offset from the first row. I tried fixing this by adjusting the Array widths slightly, some to 1000s of an inch, but that only fixed some. Any idea why this would be? Code:
End With Tbl.Rows.WrapAroundText = False Tbl.Rows.Alignment = wdAlignRowCenter End With Next End With Application.ScreenUpdating = True End Sub Last edited by CodingGuruInTraining; 09-27-2015 at 12:10 PM. Reason: now with image attached |
![]() |
Tags |
column width, combine, tables |
Thread Tools | |
Display Modes | |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
wendyloooo | Word Tables | 1 | 05-26-2015 01:19 PM |
Splitting one column into two with two different widths | officeboy09 | Excel | 6 | 12-15-2013 09:48 PM |
Pasting tables from Excel 2010 into Word 2010 - How to fix column widths? | GracieB | Word | 7 | 10-02-2013 06:24 AM |
Adjusting column widths | norwood | Word VBA | 0 | 09-24-2013 06:53 AM |
![]() |
molesy | Excel | 3 | 09-16-2013 12:51 PM |