从具有合并单元格的表格中删除列 [英] Deleting columns from a table with merged cells

查看:122
本文介绍了从具有合并单元格的表格中删除列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从具有水平合并单元格的表格中删除列

I'm trying to delete columns from a table which has horizontally merged cells

Selection.MoveEnd Unit:=WdUnits.wdCell, Count:=3
Selection.Columns.Delete

即使列被删除,合并的单元格也会在此过程中被删除,留下损坏的表格.

Eventhough columns are getting deleted, merged cells are removed in the process leaving a broken table.

几乎类似的删除行的方法工作正常,如本答案

Almost similar approach to delete rows works fine as explained in this answer

解决方法

我正在做这样的事情来解决

I'm doing something like this as work around

Selection.MoveEnd Unit:=WdUnits.wdCell, Count:=3
Selection.MoveDown Unit:=WdUnits.wdLine, Count:=2, Extend:=wdExtend
Selection.Cells.Delete

然后将索引 1,2 处的单元格宽度设置为表格行的其余部分.这样你就可以避免合并的单元格被删除.

Then setting width of Cell at index 1,2 to rest of the table rows. This way you can avoid merged cell getting deleted.

推荐答案

Word 表格并不总是直观的.如果一个单元格跨越要删除的列,那么整个跨越的单元格将始终被删除,如您所示.

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.

当我不使用 VBA 时,我总是在删除行或列之前取消合并单元格;否则 Word 的行为很难预测.

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

使用 VBA 我建议如下:

Using VBA I would suggest the following:

'splits the header row of the current table into 7 cells
Selection.tables(1).cell(1,2).split numrows:=1, numcolumns:=7

'your code to delete columns
Selection.MoveEnd Unit:=WdUnits.wdCell, Count:=3
Selection.Columns.Delete

'merge the header row back into one span
ActiveDocument.Range( _
    start:= Selection.tables(1).cell(1,2).range.start, _
    end :=  Selection.tables(1).cell(1,5).range.end _
    ).cells.Merge

或者对于更通用的方法,删除 n 列:

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

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

这篇关于从具有合并单元格的表格中删除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆