繁体   English   中英

垂直合并细胞破坏宏

[英]Vertially Merged Cells breaking macro

我发现此宏可以遍历文档并从表中删除小数点。 但是,当遇到带有垂直合并的单元格的表时,它将中断。 有没有一种方法可以解决此问题而不删除或取消合并单元格?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM