Hello,
I've been working on code to highlight certain rows in a Word table based on the values in the 1st column. If the text in the first column is same as the text in the cell above it, then keep coloring that row a certain color. If the text in the first column
is not the same as the text in the cell above it, then color that row a different color.
Problem:
The code works if I use F8 to step through it, but it gives the Run Time Error 5941 Error - Requested Member of Collection Does Not Exist when I run the code. On debug, it highlights the line below:
aTable.Rows(r).Shading.BackgroundPatternColor = color
When I debug and step through it step by step, I think the problem is when r gets to past the end of the table to r=10 (my table only has 9 rows including the header), it tries to apply the color, and it gives the error.
What I've tried:
- My code starts by saying, Do While aTable.Cell(r,1).Range.Text <>"". I don't see why that wouldn't stop running the code when it hits a row that doesn't exist. I would think that register as "" (empty) blank and would stop running the code.
- I've also changed the Loop command at the end to say Loop Until r is Nothing, and I've also changed it to Loop Until aTable.Cell(r, 1).Range.Text is Nothing. They both give Type Mismatch error.
Could someone help with how to fix this? As I say it works, but when it hits that the row outside of the table, row 10, which does not exist, it errors out. How can I make the code stop when it reaches the last row? Thank you for your help.
| HUB |
NODE A |
NODE B |
NODE C |
NODE D |
Helper |
SEGMENT TYPE |
Proposed Dates |
Comments |
| Plite |
D1111 |
D2222 |
D3333 |
D4444 |
|
4 |
7/11/2017 |
(row green) |
| Plite |
D5555 |
D6666 |
?? |
?? |
Y |
2 |
7/11/2017 |
(row green) |
| Aleive |
E3333 |
E3333 |
E33333 |
E33333 |
|
1 |
7/12/2017 |
(row no color) |
| Aleive |
E3333 |
|
|
|
Y |
4 |
7/12/2017 |
(row no color) |
| Aleive |
E3333 |
Add to E43333 |
|
|
|
14 |
7/12/2017 |
(row no color) |
| Eagle |
L3333 |
L3333 |
|
|
|
1 |
7/13/2017 |
(row green) |
| Eagle |
L3333 |
L13333 |
L13333 |
L13333 |
|
14 |
7/13/2017 |
(row green) |
| Grease |
J33333 |
J3333 |
|
|
|
12 |
7/14/2017 |
(row no color) |
Here is the full code:
Sub ColorTablesMtgMinutes()
Dim aTable As Table
Dim currenttableindex As Integer
Dim color As WdColorIndex
Dim r As Long
Dim colorit As Boolean
'tells which number table I have my cursor in
currenttableindex = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count
Set aTable = ActiveDocument.Tables(currenttableindex) '4 is the reschedules table in the meeting
'color 1st row, headings, slightly darker green
If ActiveDocument.Tables.Count >= 1 Then
With aTable.Rows(1).Shading
.BackgroundPatternColor = RGB(197, 224, 179)
End With
End If
'sets colorit to False at first
colorit = False
With aTable
r = 2 'data starts in row 2
Do While aTable.Cell(r, 1).Range.Text <> ""
'1st determine if cell value in cell is = to cell value in cell above
If aTable.Cell(r, 1).Range.Text <> aTable.Cell(r - 1, 1).Range.Text Then
colorit = Not colorit
End If
'then determine color to use on this row, light green or no shading
If colorit Then
color = RGB(226, 239, 217)
Else
color = RGB(255, 255, 255)
End If
'Apply coloring to row
aTable.Rows(r).Shading.BackgroundPatternColor = color
'point to next row of data
r = r + 1
Loop
End With
End Sub