How to remove empty lines or lines with spaces in a table in which one or more cells are vertically merged?

I need a vba script or help in what I write so as not to get out of the iteration when the table contains vertically and horizontally merged cells .

Example table:

--------- | | | <-- I don't want these rows deleted, they can be skipped |---| | | | | <-- I don't want these rows deleted, they can be skipped |---|---| | | | <-- this must be checked for emptiness in order to decide to delete or not |---|---| | | | <-- this must be checked for emptiness in order to decide to delete or not |---|---| 

My script in VBA:

 Public Sub DeleteEmptyRows() Dim c As String c = "" Dim oTable As Table, oRow As Integer ' Specify which table you want to work on. For Each oTable In ActiveDocument.Tables For oRow = oTable.Rows.Count To 1 Step -1 'On Error GoTo NextIteration MsgBox oTable.Rows(oRow).Range.Text 'If Len(oTable.Rows(oRow).Range.Text) = oTable.Rows(oRow).Cells.Count * 2 + 2 Then If Len(Replace(oTable.Rows(oRow).Range.Text, Chr(13) & Chr(7), vbNullString)) = 0 Then oTable.Rows(oRow).Delete End If Next oRow Next oTable MsgBox c End Sub 

How to reproduce the error: Create a 5x5 table. Select cell (0,0) and cell (1, 0) and combine them. Select cell (0, 1) and cell (0, 2) and merge. Run the script and get error 5991.

The problem is that I get a 5991 runtime error: I cannot access individual rows in this collection because there are vertically merged cells.

I really don't know what to do, because if this error does not happen, no line will look after. Usually my tables have a header that has vertically merged cells, but no body rows, so I can do nothing ...

for word.

+1
source share
2 answers

This is what I came up with to delete all rows in a table that do not contain stacked cells and do not contain any text.

The problem with tables containing merged cells is not so much in deleting the rows, but in determining which cells are actually merged, and then deleting the ones left.

The way I turned to this was to scroll through all the cells in the table and for each training row, how many columns were counted (horizontally grouped cells and cells vertically merged from above are ignored) and thanks to this page ( http: // word.mvps.org/FAQs/MacrosVBA/GetRowColSpan.htm ) if any of the cells in the row is the top of the vertically merged cell that we can say.

Finally, we also check to see if there is any text in the line.

This is the code I came up with, hopefully with comments it should be easy. Unfortunately, due to the way Word deals with this material, cells must choose, and not just use ranges - this is not ideal, because it slows down significantly. He worked in all my tests.

 Option Explicit Public Sub DeleteEmptyRows() Dim oTable As Table, oCol As Integer, oRows As Integer Dim iMergeCount() As Integer, dCellData() As Double Dim MyCell As Cell Dim iCurrentRow As Integer, iRowCounter As Integer 'Watching this happen will slow things down considerably Application.ScreenUpdating = False ' Specify which table you want to work on. For Each oTable In ActiveDocument.Tables 'We need to store the number of columns to determine if there are any merges oCol = oTable.Columns.Count ReDim dCellData(1 To oTable.Rows.Count, 1 To 3) 'The first column will count the number of columns in the row if this doesn't match the table columns then we have merged cells 'The second column will count the vertical spans which tells us if a vertically merged cell begins in this row 'The third column will count the characters of all the text entries in the row. If it equals zero it empty. iCurrentRow = 0: iRowCounter = 0 For Each MyCell In oTable.Range.Cells 'The Information property only works if you select the cell. Bummer. MyCell.Select 'Increment the counter if necessary and set the current row If MyCell.RowIndex <> iCurrentRow Then iRowCounter = iRowCounter + 1 iCurrentRow = MyCell.RowIndex End If 'Check column index count If MyCell.ColumnIndex > VBA.Val(dCellData(iRowCounter, 1)) Then dCellData(iRowCounter, 1) = MyCell.ColumnIndex 'Check the start of vertically merged cells here dCellData(iRowCounter, 2) = dCellData(iRowCounter, 2) + (Selection.Information(wdEndOfRangeRowNumber) - Selection.Information(wdStartOfRangeRowNumber)) + 1 'Add up the length of any text in the cell dCellData(iRowCounter, 3) = dCellData(iRowCounter, 3) + VBA.Len(Selection.Text) - 2 '(subtract one for the table and one for cursor(?)) 'Just put this in so you can see in the immediate window how Word handles all these variables Debug.Print "Row: " & MyCell.RowIndex & ", Column: " & MyCell.ColumnIndex & ", Rowspan = " & _ (Selection.Information(wdEndOfRangeRowNumber) - _ Selection.Information(wdStartOfRangeRowNumber)) + 1 Next MyCell 'Now we have all the information we need about the table and can start deleting some rows For oRows = oTable.Rows.Count To 1 Step -1 'Check if there is no text, no merges at all and no start of a vertical merge If dCellData(oRows, 3) = 0 And dCellData(oRows, 1) = oCol And dCellData(oRows, 2) = oCol Then 'Delete the row (we know it totally unmerged so we can select the first column without issue oTable.Cell(oRows, 1).Select Selection.Rows.Delete End If Next oRows Next oTable Application.ScreenUpdating = True End Sub 
+2
source

You should check the properties of the Range.MergeCells conditions that will return TRUE if the cells in the range are merged.

+1
source

All Articles