VBA Macro: Calculate Total Rows in Word Tables
This VBA macro calculates the total number of rows in all tables in the current Word document selection and prints it to the Immediate Window. It also includes a loop that can be used to perform additional actions on each table, such as modifying cell contents.
To use this macro, open the Visual Basic Editor in Word and paste the code into a new module. Then, select the tables in your document that you want to include in the count and run the macro.
Note that this macro assumes that the selection only contains tables and no other types of content. If there are other types of content in the selection, the macro may not work correctly. Also, the loop that modifies cell contents is currently commented out; remove the apostrophes to activate it.
Sub sum()
Dim i As Integer
Dim sum As Integer
sum = 0
Debug.Print ('表格数:' & Word.Selection.Tables.Count)
For i = 1 To Word.Selection.Tables.Count
sum = sum + Word.Selection.Tables.Item(i).Rows.Count - 1
Dim thisTable As Table
Set thisTable = Word.Selection.Tables.Item(i)
Dim j As Integer
For j = 2 To thisTable.Rows.Count
'thisTable.Cell(j, 4).Range.Text = "一致"
Next
Next
Debug.Print ('用例数:' & sum)
End Sub
原文地址: https://www.cveoy.top/t/topic/njFq 著作权归作者所有。请勿转载和采集!