首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何循环遍历具有拆分单元格的word表格中的所有单元格

如何循环遍历具有拆分单元格的word表格中的所有单元格
EN

Stack Overflow用户
提问于 2013-03-23 17:39:45
回答 3查看 18K关注 0票数 10

我在MS word中有一个3X3(比如tableA)表格。第(2,2)个单元格是拆分的单元格(拆分成2X2表)。如何遍历tableA中的所有单元格。

Dim strCellText As String
Dim uResp As String
Dim Row As Integer
Dim Col As Integer

Dim itable As Table


For Each itable In ThisDocument.Tables

    uResp = ""

    For Row = 1 To itable.Rows.Count

        For Col = 1 To itable.Columns.Count

            strCellText = itable.Cell(Row, Col).Range.Text
            uResp = uResp & Trim(strCellText)                

        Next

    Next

    MsgBox uResp
Next

这个程序给出了一个编译错误:

Run time error 5914
The requested member of the collection does not exist

如何遍历具有拆分单元格的表格的单元格。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-23 18:45:10

您应该假设每一行都有最大可能的列数。在你的情况下,应该是四个。为了遍历每个单元格,我建议在第一个循环开始之前设置On Error Resume Next。然后在你的内部循环中,试试下面的代码:

strCellText = itable.cell(Row, Col).Range.Text

If Err.Number = 0 Then
    uResp = uResp & Trim(strCellText)
    Debug.Print Row, Col, strCellText
Else
    Err.Clear
End If
票数 7
EN

Stack Overflow用户

发布于 2018-08-23 01:49:55

如果你想浏览MS Word文档中所有表格中的所有单元格,即使单元格被合并,我也能得到我想要的结果。试试这个:

Sub CheckingInTheCell
Dim C as Cell
Dim tableCount, Ctr

tableCount = ActiveDocuments.tables.count

for Ctr = 1 to tableCount
   For each C in ActiveDocument.Tables(Ctr).Range.cells
       .....your validations or whatever you would like to do for the cell content
   next C
next Ctr

End Sub
票数 3
EN

Stack Overflow用户

发布于 2015-05-20 19:12:54

当我试图从(有时是格式错误的)表中提取数据时,我会遇到这种情况。这是我处理它的方式:

检查列数

For each row in table.rows
  if row.cells.count < expectedRows
    'You know you are lacking rows
  else
    'Normal processing
  end if
Next

或者,如果您想要所有数据,请遍历每个单元格

For each row in table.rows
  For each cell in row.cells
    'Process individual cells
  Next
Next

如果单元格是垂直合并的,这些都不起作用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15585279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档