垂直合并的单元格打破宏



我发现这个宏可以浏览文档并从表格中删除小数点。但是,当它遇到具有垂直合并单元格的表格时,它会中断。有没有办法在不删除或取消合并单元格的情况下解决这个问题?

Sub RoundAllNumbersInTables()
Dim currentTbl As Table
Dim currentCl As Cell
Dim currentRow As Row
Dim currentText As String
For Each currentTbl In ActiveDocument.Tables
For Each currentRow In currentTbl.Rows
For Each currentCl In currentRow.Cells
currentText = Trim(Left(currentCl.Range.Text, Len(currentCl.Range.Text) - 2))
If IsNumeric(currentText) Then
currentCl.Range.Text = Format(Round(currentText, 0), "0")
End If
Next
Next
Next
End Sub

如果我现在运行它,我会得到Run time error:5991

Cannot access individual rows in the collection because the table has vertically merged cells.

如果只有垂直合并的单元格,则可以从按行分析表切换到按列分析表。

Dim currentCol As Column
For Each currentTbl In ActiveDocument.Tables
For Each currentCol In currentTbl.Columns
For Each currentCl In currentCol.Cells

但是,由于您并不总是知道表是否合并了单元格,因此最好的选择是循环访问currentTbl.Range.Cells

For Each currentTbl In ActiveDocument.Tables
For Each currentCl In currentTbl.Range.Cells
currentText = Trim(Left(currentCl.Range.Text, Len(currentCl.Range.Text) - 2))
If IsNumeric(currentText) And Left$(currentText, 1) = "$"  Then
currentCl.Range.Text = Format(Round(currentText, 0), "$#,###")
End If
Next
Next

最新更新