Consulting

Results 1 to 12 of 12

Thread: Horizontally merged cells

  1. #1
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location

    Horizontally merged cells

    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/show...y-merged-cells

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Thanks for the reply

    Quote Originally Posted by macropod View Post
    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?

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    Paul thanks for the discussion. I think I have a starting point. I will post when I get started.

  8. #8
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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.

  9. #9
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  10. #10
    VBAX Tutor
    Joined
    Jul 2016
    Posts
    266
    Location
    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.

  11. #11
    VBAX Newbie
    Joined
    Feb 2022
    Posts
    1
    Location
    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

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    4 yo thread is now closed
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •