gpt4 book ai didi

vba - 复制带有合并单元格的表格行

转载 作者:行者123 更新时间:2023-12-05 01:11:00 26 4
gpt4 key购买 nike

我需要插入如下所示格式的一组行的多个副本。

+------------------------------------------------- +
| 1,1 |
+------------------------------------------------- +
| | 2,2 | | 2,4 | |
| 2,1 +---------+ 2,3 +---------+ 2,5 |
| | 3,2 | | 3,4 | |
+------------------------------------------------- +
| 4,1 |
+------------------------------------------------- +
| | 5,2 | | 5,4 | |
| 5,1 +---------+ 5,3 +---------+ 5,5 |
| | 6,2 | | 6,4 | |
+------------------------------------------------- +

在情况 A 中,我需要在第 4 行之前插入第 1-3 行的多个副本。

在情况 B 中,我需要在表的末尾插入第 4-6 行的多个副本。

table.rows(n) 方法不起作用并给出以下错误:

Run-time error '5991':

Cannot access individual rowsin this collection because the table has vertically merged cells



但是,可以从用户界面执行此操作,因此它必须是可能的!

最佳答案

这是我如何克服这个问题(在 Word 2007 和 Word 2003 中测试)

Sub CaseA()

Dim D As Document, T As Table

Set D = ActiveDocument

Set T = D.Tables(1) ' select the first table in the document
' select from the start of row 1 to the start of row 4
D.Range(T.Cell(2, 1).Range.Start, T.Cell(4, 1).Range.Start).Select

Selection.Copy ' copy the rows
' move the insertion point to the start of row 4
Selection.Collapse wdCollapseEnd

Selection.Paste ' insert a copy
' (can do this as many times as you want)

End Sub

Sub CaseB()

Dim D As Document, T As Table

Set D = ActiveDocument

Set T = D.Tables(1)
' select from the start of row 5 to the end of the last cell present in row 6
' plus 1 character for column 5 plus 1 character to move outside the table
D.Range(T.Cell(5, 1).Range.Start, T.Cell(6, 4).Range.End + 2).Select

Selection.Copy ' copy the rows
' move the insertion point just outside the table
Selection.Collapse wdCollapseEnd

Selection.Paste ' insert a copy

End Sub

关于vba - 复制带有合并单元格的表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14614034/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com