VBA 合并两个单元格的代码

VBA code to merge two cells together

我的场景是一个包含一个两列的文档 table。我正在从数据库中读取并填充每个 row/column,但对于某些记录,我想将两列合并到一个单元格中并将其填充为一行,然后继续两列方案。我想发出 VBA 语句将两个单元格合并在一起,使一个单元格横跨整行。这不可能把"record"当作一个新的宏。我不知道运行时的行号是多少,我只知道列号。我的搜索字符串是:'"Word 2013" VBA table 合并单元格' 但我得到了很多网站,向您展示了如何手动执行此操作,而不是使用 VBA 代码。

我当前的代码:

With ActiveDocument.Tables(1)
.Cell(Row:=1, Column:=1).merge _
MergeTo:=.Cell(Row:=1, Column:=2)
.Borders.Enable = True
End With

在文档中,我有一行两列的小table。但是我应该能够有一个 table 的三行和任意混合的列,对吧?我只想选择任意两列并将它们合并成一个 space,但在 运行 时我不知道要提供什么行号。

我终于解决了这个问题,我想再次感谢你的帮助。这是我当前的代码。整个文档是一个两栏 table,瘦小的文档,包含 hyperlink,因此可以在 iPhone 上阅读和导航。当数据中的Unit值发生变化时,我想1)在下面插入一个新行并将其合并为一列,添加一个'Go Home' link,然后继续添加两列行。

文档最终转换为 .PDF 并被 iOS 位用户访问。

    'Add a row for a 'back to home' link 
    If (intUnitOrder > intCurrentUnit) Then
       Selection.InsertRowsBelow (1)
       rowno = Selection.Information(wdEndOfRangeRowNumber) - 1
       With ActiveDocument.Tables(1)
         .Cell(Row:=rowno, Column:=1).Merge MergeTo:=.Cell(Row:=rowno, _ 
         Column:=2)
       End With

       Selection.Tables(1).Rows(rowno).Range.ParagraphFormat. _
         Alignment = wdAlignParagraphRight
       Selection.Shading.BackgroundPatternColor = RGB(230, 230, 230)
       Selection.Font.ColorIndex = wdBlue
       Selection.Font.Italic = True

       ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
         Address:="#Home", SubAddress:="", _
         ScreenTip:="Go back to the top", _
         TextToDisplay:="back to Home"

       Selection.MoveDown wdLine, 1
       intCurrentUnit = intUnitOrder
    End If