Deleting columns from a table with merged cells Deleting columns from a table with merged cells vba vba

Deleting columns from a table with merged cells


Word tables are not always intuitive. If a cell spans a column that is to be deleted, then the ENTIRE spanned cell will always be deleted, as you have shown.

When I'm NOT using VBA, I always unmerge cells before deleting rows or columns; otherwise Word's behavior is hard to predict.

Using VBA I would suggest the following:

'splits the header row of the current table into 7 cellsSelection.tables(1).cell(1,2).split numrows:=1, numcolumns:=7'your code to delete columnsSelection.MoveEnd Unit:=WdUnits.wdCell, Count:=3Selection.Columns.Delete'merge the header row back into one spanActiveDocument.Range( _    start:= Selection.tables(1).cell(1,2).range.start, _    end :=  Selection.tables(1).cell(1,5).range.end _    ).cells.Merge

or for a more general approach, to delete n columns:

width = Selection.tables(1).columns.count - 1Selection.tables(1).cell(1,2).split numrows:=1, _     numcolumns:= width - 1Selection.MoveEnd Unit:=WdUnits.wdCell, Count:= nSelection.Columns.DeleteActiveDocument.Range( _    start:= Selection.tables(1).cell(1,2).range.start, _    end :=  Selection.tables(1).cell(1,width-n-1).range.end _    ).cells.Merge


This should do it

Sub DeleteCols()    Dim Col2Delete As Range    Set Col2Delete = Selection.EntireColumn    Col2Delete.DeleteEnd Sub


First of all, we must look at the table not as rows/columns, but as indexed cells. (fig.1)

enter image description here

Now, we should select a range that starts from second cell and end to the last cell.

enter image description here

Finally, delete selection columns

        Set Rng = Tbl.Range.Cells(2).Range        Rng.End = Tbl.Range.Cells(Tbl.Range.Cells.Count).Range.End        Rng.Select        Selection.Columns.Delete