Share via

Word VBA to highlight rows in Word table based on criteria: Runtime Error 5941 - Requested Member of Collection Does Not Exist

Anonymous
2017-06-27T23:23:16+00:00

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:

  1. 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.
  2. 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

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
2017-06-28T03:55:45+00:00

Use:

With aTable

    For r = 2 to .Rows.Count

        If .Cell(r, 1).Range.Text <> "" And  .Cell(r, 1).Range.Text <> .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

       .Rows(r).Shading.BackgroundPatternColor = color

    Next r

End With

Was this answer helpful?

3 people found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2017-06-28T23:11:21+00:00

    Use:

    With aTable

        For r = 2 to .Rows.Count

            If .Cell(r, 1).Range.Text <> "" And  .Cell(r, 1).Range.Text <> .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

           .Rows(r).Shading.BackgroundPatternColor = color

        Next r

    End With

    Mr. Robbins, thank you so much. This version is more clear and concise, and I see where you used an AND to combine the search for the end of the table and the comparison of a cell with the cell above.

    This has helped me so much, and thanks for the lesson in cleaning up code. Thank you!!!

    Was this answer helpful?

    0 comments No comments