View Full Version : [SOLVED:] Horizontally merged cells
Kilroy
12-07-2018, 08:02 AM
Guys I have searched high and low and cannot find any information on unmerging horizontally merged cells in a word table. Unmerging vertically works can unmerging horizontally?
Unmerging vertically link:
http://www.vbaexpress.com/forum/showthread.php?59760-Unmerging-Vertically-merged-cells
macropod
12-07-2018, 10:14 PM
You need to understand that, depsite what appears to work in your other thread, Word cannot tell whether a cell has been merged with another cell or whether what appears to be a merged cell is the result of other cells being split - vertically and/or horizontally.
All the code you're now using does is to assume any differences in vertical heights are due to vertical merges; the possibility of the vertical splitting of adjoining cells isn't even considered.
For potential horizontal splits/merges, things are even more complicated, as the cells in a given column can have different widths without any splitting or merging being a factor.
Kilroy
12-07-2018, 10:44 PM
Thanks for the reply
For potential horizontal splits/merges, things are even more complicated, as the cells in a given column can have different widths without any splitting or merging being a factor.
so since my table has exact column widths:
 With oTbl
.Columns(1).SetWidth ColumnWidth:=60, _
RulerStyle:=wdAdjustFirstColumn
.Columns(2).SetWidth ColumnWidth:=200, _
RulerStyle:=wdAdjustFirstColumn
.Columns(3).SetWidth ColumnWidth:=200, _
RulerStyle:=wdAdjustFirstColumn
.Columns(4).SetWidth ColumnWidth:=60, _
RulerStyle:=wdAdjustFirstColumn
It may possible?
macropod
12-07-2018, 10:59 PM
So, if all the columns have pre-set widths, how can any of the cells be merged or split? Code such as you posted would not work on a table with horizontally merged/split cells.
Kilroy
12-07-2018, 11:09 PM
That is the code that creates the table. it cannot be used to reset the column widths after any cells are merged. I was just showing you that the columns should have a specific width. I was thinking...is it possible to split a cell that is the width of a combination of 2, 3, or 4 cells based on the above widths? so if a cell ColumnWidth:=260 or 400 or 460 or 520 then split? so if it was 260 or 400 split in two if its 460 split in three if its 520 split in 4? Just a thought.
macropod
12-08-2018, 12:11 AM
Consider the case where the user drags the right border of a cell in your 2nd column 60pt to the right. It's now 260pt wide, but nothing's been split or merged. Moreover, the corresponding cell in the 3rd column is now only 140pt wide. If such a scenario is possible, you might use:
oTbl.Columns.DistributeWidth
before resetting their widths. Other than that, you'd need to loop though all cells, finding which rows have fewer than 4 cells, then choosing one (or more) to split before using Columns.DistributeWidth, etc.
Kilroy
12-08-2018, 05:50 AM
Paul thanks for the discussion. I think I have a starting point. I will post when I get started.
Kilroy
12-13-2018, 02:13 PM
I just had a forced Microsoft update and lost all macros for the last 3 months. If I bother to replace them. I'll reopen the thread and post results. What a disappointment. Second time this has happened.
macropod
12-13-2018, 02:33 PM
That suggests you're working in a corporate environment and your IT department kindly overwrote your Normal.dotm file - in which you were storing your macros. Next time, store them in a different template and keep a backup.
Kilroy
12-13-2018, 02:51 PM
Thanks for the advice. I think you said the same thing last time. LOL. IT guy here is a knob. He can't stand that I write macros. He says it's his job but the stuff I'm doing is above his knowledge.
hackman
02-17-2022, 04:44 PM
If you are OK with just adding cells at the end of row (as opposed to unmerging specific cells) and setting the cell width to match existing columns, try the code below. 
It errors on tables with vertically merged cells so might need to run SplitVerticalMerge (from the other thread) first.
Sub SplitHorizMerge()    
    Dim myTable As Table, myCell As Cell
    Dim myRow As Row, maxCol As Long, maxColIndex As Long, myItem As Variant
    Dim myCounter, OrigCellCount As Long
    Dim CellsCol As New Collection
        
    Set myTable = ActiveDocument.Tables(1)
    With myTable
      'identify row with highest number of cells
      For Each myRow In .Rows
         If myRow.Cells.Count > maxCol Then
            maxCol = myRow.Cells.Count
            maxColIndex = myRow.index
         End If
      Next
      
      'collect the width for each cell in the row identified above
      For Each myCell In .Rows(maxColIndex).Cells
         CellsCol.Add myCell.Width
      Next
      
      'apply changes to table
      For Each myRow In .Rows
         With myRow
            OrigCellCount = .Cells.Count
            If .index = maxColIndex Then 'skip identified row
            ElseIf OrigCellCount = maxCol Then 'skip rows that already have same number of cells            
            Else
               'split last cell
               .Cells(.Cells.Count).Split 1, maxCol - OrigCellCount + 1
               For myCounter = 1 To maxCol
                  'set the width of each cell to match identified row
                  .Cells(myCounter).Width = CellsCol(myCounter)
                  
                  ' add some text into the newly added cells
                  If myCounter > OrigCellCount Then
                     .Cells(myCounter).Range.Text = .Cells(myCounter - 1).Range.Text & " <SplitCell>"
                  End If
                  
               Next
               
            End If
            
         End With
         
      Next
      
    End With
End Sub
4 yo thread is now closed
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.