通过VBA访问单词表中的拆分单元格

问题描述 投票:0回答:1

在Word文档中具有5X7表,第7列分为5行。如何通过VBA访问此拆分单元格的每一行?

Example table

ms-word word-vba
1个回答
0
投票

当您以这种方式拆分单元格时,每一行将成为表中的新行。因此,您将在第7列中处理单元格,就好像没有拆分一样。拆分下方的第一行是所有列的第6行。以下宏将帮助您在表格中识别任何选定的单元格(或多个单元格)的地址/范围:

Sub CellRange()
Dim StrAddr As String
' This macro displays the address of a table's selected cell range
' and the table's last cell address on Word's Status Bar (Word 2010 & earlier)
' or in a Message Box (Word 2013 & later)
With Selection
  If .Information(wdWithInTable) = True Then
    StrAddr = "The selected "
    If .Cells.Count = 1 Then
      StrAddr = StrAddr & "cell address is: "
    Else
      StrAddr = StrAddr & "cells span: "
    End If
    StrAddr = StrAddr & ColAddr(.Cells(1).ColumnIndex) & .Cells(1).RowIndex
    If .Cells.Count > 1 Then
      StrAddr = StrAddr & ":" & ColAddr(.Characters.Last.Cells(1).ColumnIndex) & _
        .Characters.Last.Cells(1).RowIndex
    End If
    With .Tables(1).Range
      StrAddr = StrAddr & ". The table's last cell is at: " & _
        ColAddr(.Cells(.Cells.Count).ColumnIndex) & .Cells(.Cells.Count).RowIndex
    End With
  Else
    StrAddr = "The selection is not in a table!"
  End If
End With
If Int(Application.Version) < 15 Then
  StatusBar = StrAddr
Else
  MsgBox StrAddr, vbOKOnly
End If
End Sub

Function ColAddr(i As Long) As String
If i > 26 Then
  ColAddr = Chr(64 + Int(i / 26)) & Chr(64 + (i Mod 26))
Else
  ColAddr = Chr(64 + i)
End If
End Function
© www.soinside.com 2019 - 2024. All rights reserved.