#1
|
|||
|
|||
Merge table cells across columns but skip if column don't exist
I have a Word doc with tables of various column counts.
I am working on a macro that loops through every table's row 1 and merges cells in column 2 and 3, 3 and 4, 4 and 5, etc. That part works fine. My problem comes with tables that don't have that many columns. I tried adding in an if statement along the lines of if there are more than X columns, continue, but get an error that an object is required when it gets to this line: If Tbl.Columns.Count > 3 Then I'm trying to learn VBA but am not sure what the problem is. Thanks! Sub MergeCells2() For Each Tbl In ActiveDocument.Tables Tbl.Cell(1, 2).Merge (Tbl.Cell(1, 3)) Next If Tbl.Columns.Count > 3 Then For Each Tbl In ActiveDocument.Tables Tbl.Cell(1, 3).Merge (Tbl.Cell(1, 4)) Next End If If Tbl.Columns.Count > 4 Then For Each Tbl In ActiveDocument.Tables Tbl.Cell(1, 4).Merge (Tbl.Cell(1, 5)) Next End If If Tbl.Columns.Count > 5 Then For Each Tbl In ActiveDocument.Tables Tbl.Cell(1, 5).Merge (Tbl.Cell(1, 6)) Next End If If Tbl.Columns.Count > 6 Then For Each Tbl In ActiveDocument.Tables Tbl.Cell(1, 6).Merge (Tbl.Cell(1, 7)) Next End If If Tbl.Columns.Count > 7 Then For Each Tbl In ActiveDocument.Tables Tbl.Cell(1, 7).Merge (Tbl.Cell(1, 8)) Next End If If Tbl.Columns.Count > 8 Then For Each Tbl In ActiveDocument.Tables Tbl.Cell(1, 8).Merge (Tbl.Cell(1, 9)) Next End If End Sub |
#2
|
||||
|
||||
Try:
Code:
Sub CellMerge() Application.ScreenUpdating = False Dim Tbl As Table, Rng As Range For Each Tbl In ActiveDocument.Tables With Tbl.Rows(1) If .Cells.Count > 2 Then Set Rng = .Range Rng.Start = .Cells(2).Range.Start Rng.Cells.Merge End If End With Next Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thank you. I was looking to join two columns at a time, not every column into one cell, but added one line and it worked. Then repeated the set, just changing the cell numbers I wanted to merge. Thanks!
Sub CellMerge() Application.ScreenUpdating = False Dim Tbl As Table, Rng As Range For Each Tbl In ActiveDocument.Tables With Tbl.Rows(1) If .Cells.Count > 2 Then Set Rng = .Range Rng.Start = .Cells(2).Range.Start Rng.End = .Cells(3).Range.End Rng.Cells.Merge End If End With Next |
#4
|
||||
|
||||
Slightly simpler is:
Code:
Sub CellMerge() Application.ScreenUpdating = False Dim Tbl As Table For Each Tbl In ActiveDocument.Tables With Tbl.Rows(1) If .Cells.Count > 2 Then ActiveDocument.Range(.Cells(2).Range.Start, .Cells(3).Range.End).Cells.Merge End If End With Next Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Thanks! I appreciate your help!
|
Tags |
merge, skip columns, table |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How do I skip automatic Numbering on merged cells in my table? | Snaybot | Word | 1 | 09-28-2015 11:27 PM |
Column text flow with table across the columns | ravenns | Word | 3 | 07-11-2013 11:29 PM |
How to merge two columns & replace contents of cells conditionally? | mag | Excel | 3 | 10-24-2012 01:07 PM |