簡體   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