Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #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
  #2  
Old 05-25-2016, 03:35 PM
macropod's Avatar
macropod macropod is offline Merge table cells across columns but skip if column don't exist Windows 7 64bit Merge table cells across columns but skip if column don't exist Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 05-26-2016, 06:40 AM
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

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
Reply With Quote
  #4  
Old 05-28-2016, 02:26 AM
macropod's Avatar
macropod macropod is offline Merge table cells across columns but skip if column don't exist Windows 7 64bit Merge table cells across columns but skip if column don't exist Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
PS: When posting code, please use the code tags, indicated by the # button on the posting menu
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-31-2016, 06:20 AM
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

Thanks! I appreciate your help!
Reply With Quote
Reply

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 03:03 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft