如何在vba中检测合并的单元格(Word 2010) [英] how to detect merged cells in vba (Word 2010)
问题描述
我想在Word 2010上检测并跳过vba中的合并单元.
I would like to detect and skip merged cells in vba on word 2010.
我正在制作一个需要在第一栏中写的宏,但是我有一些合并的单元格,我不想写. 我发现的所有示例都是针对Excel的.
I'm making a macro which need to write in the first column but I've some merged cells and I don't want to write in. All examples I found was for Excel.
我试图检测每行的列数,但这没有用.我得到了"Error 5991 table has vertically merged cells"
.
I tried to detect the number of columns per rows but that didn't works. I got "Error 5991 table has vertically merged cells"
.
那么合并单元格后如何跳过该行?
So how can I just skip the row when I get merged cells ?
Sub test()
Dim Ro As Integer, Col As Integer
'init
Count = 1
Col = 1
For Ro = 4 To ActiveDocument.Tables(4).Rows.Count
'format
If Count < 10 Then
flag = "0"
Else
flag = ""
End If
'detect merge
If ActiveDocument.Tables(4).Rows(Ro).Cells.Count = 9 Then
ActiveDocument.Tables(4).Cell(Ro, Col).Range.Text = "R" & flag & CStr(Count)
Count = Count + 1
Else
Ro = Ro + 1
End If
Next Ro
End Sub
推荐答案
我找到了解决方案!
我用它来绕过错误:
出现错误时转到ToErrorHandler
On Error GoTo ErrorHandler
ErrorHandler: 如果Err.Number = 5991或Err.Number = 5941那么 错误清除 简历byebye 如果结束
ErrorHandler: If Err.Number = 5991 Or Err.Number = 5941 Then Err.Clear Resume byebye End If
For Ro = 4 To ActiveDocument.Tables(4).Rows.Count
[My ugly code]
#in my case i can use a cell to determinate if it's a my row is merged or not
If Len(ActiveDocument.Tables(4).Cell(line, 5).Range.Text) > 0 Then
[My Ugly Code]
再见:
Next Ro
希望我的解决方案可以帮助您...
Hope my solution can help you...
这篇关于如何在vba中检测合并的单元格(Word 2010)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!