计算特定样式在 table 的列中出现的次数
Counting the number of occurrences of a specific style, within a column of a table
我需要计算特定样式在 table 的列中出现的次数。我的程序查找整个文档中出现的次数,而不是仅在所选内容中查找。
Sub Find()
Selection.Tables(1).Columns(1).Select
With Selection.Find
.Style = "Style2"
iCount = 0
While .Execute
iCount = iCount + 1
Wend
MsgBox (iCount)
End With
End Sub
在 table 中执行查找是一个棘手的命题,因为查找具有在单元格内部 "bounce" 的令人讨厌的倾向。当我测试你的代码时,没有关于如何在 table 单元格中应用样式的信息,宏进入循环并且直到我强制它才停止。所以我对您的代码完全有效感到有点惊讶...
在列上查找的问题在于,在文档的底层结构中,列不是一组连续的字符,就像它在屏幕上显示的那样。 Word table 信息 运行 在单元格中从上到下,从左到右跨行,然后到下一行并重复。列选择是由 Word 应用程序维护的错觉。因此基于 Selection 或 Range 的宏代码不能遵循通常适用的规则。
以下对我有用。本质上,它在整个 table 内搜索,但当它命中不在指定列中的单元格时,目标范围将移动到列中的下一个单元格,并且搜索再次为 运行。仅计算列中的 "hits" 个内部单元格。
Sub FindStyleInstanceInTableColumn()
Dim iCount As Long, iCellCount As Long, iCounter As Long
Dim cel As word.Cell
Dim col As word.Column
Dim rngFind As word.Range, rngCel As word.Range
Dim bFound As Boolean
Set col = Selection.Tables(1).Columns(1)
iCount = 0
iCellCount = col.Cells.Count
iCounter = 1
Set rngCel = col.Cells(iCounter).Range
Set rngFind = rngCel.Duplicate
'Don't include end-of-cell marker
rngFind.MoveEnd wdCharacter, -1
rngFind.Select 'For debugging
With rngFind.Find
.Style = "Style2"
bFound = .Execute(wrap:=wdFindStop)
Do
rngFind.Select 'For debugging
If bFound Then
'If the found range is within a column cell
'then increase the counter
If rngFind.InRange(rngCel) Then
iCount = iCount + 1
'If the found range is not in a column cell
'then the style wasn't found in the cell so
'go to the next cell
ElseIf iCounter < iCellCount Then
iCounter = iCounter + 1
Set rngCel = col.Cells(iCounter).Range
rngFind.Start = rngCel.Start
rngFind.End = rngCel.Start
End If
rngFind.Collapse wdCollapseEnd
End If
bFound = .Execute(Format:=True, wrap:=wdFindStop)
Loop Until iCounter = iCellCount And Not bFound
End With
MsgBox (iCount)
End Sub
编辑:调整代码以考虑第一个单元格中没有命中和列的最后一个单元格中的命中。不同之处在于确保 rngFind 的起点与 rngCel 在同一单元格中。
我需要计算特定样式在 table 的列中出现的次数。我的程序查找整个文档中出现的次数,而不是仅在所选内容中查找。
Sub Find()
Selection.Tables(1).Columns(1).Select
With Selection.Find
.Style = "Style2"
iCount = 0
While .Execute
iCount = iCount + 1
Wend
MsgBox (iCount)
End With
End Sub
在 table 中执行查找是一个棘手的命题,因为查找具有在单元格内部 "bounce" 的令人讨厌的倾向。当我测试你的代码时,没有关于如何在 table 单元格中应用样式的信息,宏进入循环并且直到我强制它才停止。所以我对您的代码完全有效感到有点惊讶...
在列上查找的问题在于,在文档的底层结构中,列不是一组连续的字符,就像它在屏幕上显示的那样。 Word table 信息 运行 在单元格中从上到下,从左到右跨行,然后到下一行并重复。列选择是由 Word 应用程序维护的错觉。因此基于 Selection 或 Range 的宏代码不能遵循通常适用的规则。
以下对我有用。本质上,它在整个 table 内搜索,但当它命中不在指定列中的单元格时,目标范围将移动到列中的下一个单元格,并且搜索再次为 运行。仅计算列中的 "hits" 个内部单元格。
Sub FindStyleInstanceInTableColumn()
Dim iCount As Long, iCellCount As Long, iCounter As Long
Dim cel As word.Cell
Dim col As word.Column
Dim rngFind As word.Range, rngCel As word.Range
Dim bFound As Boolean
Set col = Selection.Tables(1).Columns(1)
iCount = 0
iCellCount = col.Cells.Count
iCounter = 1
Set rngCel = col.Cells(iCounter).Range
Set rngFind = rngCel.Duplicate
'Don't include end-of-cell marker
rngFind.MoveEnd wdCharacter, -1
rngFind.Select 'For debugging
With rngFind.Find
.Style = "Style2"
bFound = .Execute(wrap:=wdFindStop)
Do
rngFind.Select 'For debugging
If bFound Then
'If the found range is within a column cell
'then increase the counter
If rngFind.InRange(rngCel) Then
iCount = iCount + 1
'If the found range is not in a column cell
'then the style wasn't found in the cell so
'go to the next cell
ElseIf iCounter < iCellCount Then
iCounter = iCounter + 1
Set rngCel = col.Cells(iCounter).Range
rngFind.Start = rngCel.Start
rngFind.End = rngCel.Start
End If
rngFind.Collapse wdCollapseEnd
End If
bFound = .Execute(Format:=True, wrap:=wdFindStop)
Loop Until iCounter = iCellCount And Not bFound
End With
MsgBox (iCount)
End Sub
编辑:调整代码以考虑第一个单元格中没有命中和列的最后一个单元格中的命中。不同之处在于确保 rngFind 的起点与 rngCel 在同一单元格中。