如何访问嵌套在另一个表中的范围内的表



为了访问范围内的单个表(比如rngOuter),我使用了:tblOuter=rngOuter.Tables[1];在我将嵌套表放置在外部范围的表中的一个范围(比如rngInner)内后,我发现:tblInner=rngInner.Tables[1];不起作用。Tables[1]引用的是tblOuter,而不是表本身。事实上,rngInner的Tables集合只有一个元素,那就是tblOuter。为了访问tblInner,我必须访问tblOuter.Range.Tables[1].

有人知道我是在犯错误吗?还是事实就是这样?

AFAIK"就是这样",但您可以使用Cell.tables而不是Cell.Range.tables来查找包含表的单元格。例如,要查找当前选择中包含表的单元,您可以使用

Sub listInnerTables()
Dim c As Cell
Dim r As Range
Dim t As Table
Dim tcount As Long
Set r = Selection.Range
If r.Tables.Count > 0 Then
  tcount = 0
  For Each t In r.Tables
    tcount = tcount + 1
    For Each c In t.Range.Cells
      If c.Range.InRange(r) Then
        If c.Tables.Count > 0 Then
          Debug.Print "Table: " & CStr(tcount) & _
            vbTab & " Row: " & CStr(c.RowIndex) & _
            vbTab & " Col: " & CStr(c.ColumnIndex) & _
            vbTab & " Table count: " & CStr(c.Tables.Count)
        End If
      End If
    Next
  Next
End If
Set r = Nothing
End Sub

最新更新