|
本帖最后由 batmanbbs 于 2023-6-17 23:55 编辑
- Sub 校验合计()
- '
- ' 遍历所有表格的所有单元格(支持合并单元格)
- '
- Dim myTB As Table
- Dim Total(), Number#, Text$, i%, LastRow%
- Dim isAdd As Boolean
- For Each myTB In ActiveDocument.Tables
- With myTB.Range
- isAdd = True: LastRow = .Rows.Count
- ReDim Total(2 To .Columns.Count)
- For i = 1 To .Cells.Count
- With .Cells(i)
- If .RowIndex > 1 And .ColumnIndex > 1 Then ' 跳过第一行和第一列
- Text = Replace(Replace(Replace(.Range, ",", ""), " ", ""), Chr(13), "")
- Text = Left(Text, Len(Text) - 1)
- If IsNumeric(Text) Then
- Number = Val(Text)
- If .RowIndex = LastRow Then ' 合计行比较总数
- If Round(Total(.ColumnIndex), 2) = Round(Number, 2) Then
- .Shading.BackgroundPatternColorIndex = wdAuto
- Else
- .Shading.BackgroundPatternColorIndex = wdYellow
- End If
- Else
- Total(.ColumnIndex) = Total(.ColumnIndex) + IIf(isAdd, Number, -Number)
- End If
- End If
- Else
- If .ColumnIndex = 1 And .RowIndex <> 1 Then _
- isAdd = Left(Replace(.Range, " ", ""), 2) <> "减:"
- End If
- End With
- Next i
- End With
- Next myTB
- Set myTB = Nothing
- Erase Total
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|