Share via

Split vertically merged cells

Anonymous
2011-07-01T20:22:22+00:00

Is there a way for VBA to detect and vertically split vertically merged cells in a Word table into the proper number of rows such that the table is left without any vertically merged cells?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2011-07-02T12:19:32+00:00

Well, yes an no.

If you have only vertically merged cells we can detect them, but if there is just one horizontal merged cell you are lost.

Make a new file, create table with 5 columns and 3 rows and execute the macro below. You can see the row/column number of each cell in the MsgBox.

Now try to merge cells vertically and run the macro again. You can see a "X|X" at the position where a cell is missing. Theoretically we can use the cell above the "X|X" and split them into rows.

But now merge just 2 cells horizontally and run the macro again. No chance anymore.

Andreas.

Sub Test()

  Dim i As Long, j As Long

  Dim Data()

  Dim T As Table

  Dim C As Cell

  Dim S As String

  If Not Selection.Information(wdWithInTable) Then

    MsgBox "Selection is not within a table"

    Exit Sub

  End If

  Set T = Selection.Tables(1)

  With T

    'Build array for each cell

    ReDim Data(1 To .Rows.Count, 1 To .Columns.Count)

    'Start with the first cell

    Set C = .Cell(1, 1)

    'Visit all cells

    Do While Not C Is Nothing

      'Write the row and column into out array

      Data(C.RowIndex, C.ColumnIndex) = C.RowIndex & "," & C.ColumnIndex

      'Get the next cell

      Set C = C.Next

    Loop

    'Build a string with the row/columns from our array

    For i = 1 To UBound(Data)

      For j = 1 To UBound(Data, 2)

        S = S & IIf(IsEmpty(Data(i, j)), "X,X", Data(i, j)) & "|"

      Next

      S = S & vbCrLf

    Next

    MsgBox S

  End With

End Sub

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2011-07-05T20:08:19+00:00

    Yes, I see, thanks. Guess I'll just have to detect the 5991 error and put up a message telling the person running the macro to remove the vertical merges manually.

    Was this answer helpful?

    0 comments No comments