PDA

View Full Version : [SOLVED:] Border in word table with vertically merged cells



epattee
11-08-2018, 02:47 PM
Hello,

I am trying to have a macro button on my toolbar that will place a thick gray border on the top of the row my cursor is in. I am running into an error that states:

"Run-time error '5991'
Cannot access individual rows in this collection because the table has vertically merged cells."

This is a very helpful error code, but I don't know how to fix my code to avoid this problem. This error occurs even if my cursor is not in a row that contains merged cells, but somewhere else in the table does.

This is my code:

Private Sub darkborderrow()
Dim oRow As Row
Set oRow = Selection.Rows(1)
With oRow.Range.Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth300pt
.Color = wdColorGray60
End With
End Sub

I will also attach a sample document with tables and code that this problem would occur with incase that would be helpful.
23174

Thank you for all of your help,
Emily

macropod
11-08-2018, 08:02 PM
Try:

Sub DarkBorderRow()
Dim r As Long, c As Long
With Selection
r = .Cells(1).RowIndex
With .Tables(1).Range
For c = 1 To .Cells.Count
With .Cells(c)
If .RowIndex = r Then
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth300pt
.Color = wdColorGray60
End With
ElseIf .RowIndex > r Then
Exit For
End If
End With
Next
End With
End With
End Sub

epattee
11-09-2018, 09:02 AM
That worked!

Thank you Paul!