Vba 垂直合并的单元破坏宏
我发现这个宏可以遍历文档并从表中删除小数点。但是,当它遇到具有垂直合并单元格的表时,它会中断。有没有办法在不删除或不合并单元格的情况下解决这个问题Vba 垂直合并的单元破坏宏,vba,ms-word,Vba,Ms Word,我发现这个宏可以遍历文档并从表中删除小数点。但是,当它遇到具有垂直合并单元格的表时,它会中断。有没有办法在不删除或不合并单元格的情况下解决这个问题 Sub RoundAllNumbersInTables() Dim currentTbl As Table Dim currentCl As Cell Dim currentRow As Row Dim currentText As String For Each currentTbl In ActiveD
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
如果我现在运行它,我会得到运行时错误: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
这帮了大忙,谢谢。我使用这个宏从表中删除小数点,但是我只希望它从开头带有$符号的文本中删除小数点。有没有办法把这一点融入到我们的生活中?currentCl.Range.Text=Format(Round(currentText,0),“$”、“$”)我将此行更改为此。这将按我所需的方式格式化更改后的文本,但它仍将选择开头不带$的数字。@Jack我已编辑了答案,以包含对$符号的测试。