Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 05-25-2016, 01:45 PM
jhearing jhearing is offline Merge table cells across columns but skip if column don't exist Mac OS X Merge table cells across columns but skip if column don't exist Office for Mac 2011
Novice
Merge table cells across columns but skip if column don't exist
 
Join Date: May 2016
Posts: 3
jhearing is on a distinguished road
Default 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
Reply With Quote
 

Tags
merge, skip columns, table



Similar Threads
Thread Thread Starter Forum Replies Last Post
Merge table cells across columns but skip if column don't exist How do I skip automatic Numbering on merged cells in my table? Snaybot Word 1 09-28-2015 11:27 PM
Merge table cells across columns but skip if column don't exist 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

Other Forums: Access Forums

All times are GMT -7. The time now is 06:49 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