By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 774 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

End if without block if error - Word VBA macro

P: 1
My code (below) is giving me an "End if without block if" Error.

I'm trying to highlight a row in a table according to the first cell. Can anyone help me?

Expand|Select|Wrap|Line Numbers
  1. Sub colorrowcellone()
  2. With aTable
  3.     For r = 2 To .Rows.Count
  4.         If .Cell(r, 1).Range.Text <> "W5" Then colorit = Not colorit
  5.         End If
  6.        'then determine color to use on this row, light green or no shading
  7.         If colorit Then
  8.             Color = RBG(255, 255, 255)
  9.         Else
  10.             Color = RBG(220, 230, 241)
  11.         End If
  12.        .Rows(r).Shading.BackgroundPatternColor = Color
  13.     Next r
  14. End With
  15. End Sub
Jan 25 '19 #1

✓ answered by Seth Schrock

Here is your code with the required code tags:
Expand|Select|Wrap|Line Numbers
  1. Sub colorrowcellone()
  2. With aTable
  3. For r = 2 To .Rows.Count
  4. If .Cell(r, 1).Range.Text <> "W5" Then colorit = Not colorit
  5. End If
  6. 'then determine color to use on this row, light green or no shading
  7. If colorit Then
  8. Color = RBG(255, 255, 255)
  9. Else
  10. Color = RBG(220, 230, 241)
  11. End If
  12. .Rows(r).Shading.BackgroundPatternColor = Color
  13. Next r
  14. End With
  15. End Sub
Because you have the THEN action in line 4 on the same line as the IF, you don't need the End If on line 5. So you need to do either
Expand|Select|Wrap|Line Numbers
  1. If .Cell(r, 1).Range.Text <> "W5" Then
  2.     colorit = Not colorit
  3. End If
Or remove line 5.

I would also highly recommend using indentation to make your code easier to read. For example,
Expand|Select|Wrap|Line Numbers
  1. Sub colorrowcellone()
  2.     With aTable
  3.         For r = 2 To .Rows.Count
  4.             If .Cell(r, 1).Range.Text <> "W5" Then 
  5.                 colorit = Not colorit
  6.             End If
  7.  
  8.             'then determine color to use on this row, light green or no shading
  9.             If colorit Then
  10.                 Color = RBG(255, 255, 255)
  11.             Else
  12.                 Color = RBG(220, 230, 241)
  13.             End If
  14.  
  15.             .Rows(r).Shading.BackgroundPatternColor = Color
  16.         Next r
  17.     End With
  18. End Sub

Share this Question
Share on Google+
1 Reply


Seth Schrock
Expert 2.5K+
P: 2,941
Here is your code with the required code tags:
Expand|Select|Wrap|Line Numbers
  1. Sub colorrowcellone()
  2. With aTable
  3. For r = 2 To .Rows.Count
  4. If .Cell(r, 1).Range.Text <> "W5" Then colorit = Not colorit
  5. End If
  6. 'then determine color to use on this row, light green or no shading
  7. If colorit Then
  8. Color = RBG(255, 255, 255)
  9. Else
  10. Color = RBG(220, 230, 241)
  11. End If
  12. .Rows(r).Shading.BackgroundPatternColor = Color
  13. Next r
  14. End With
  15. End Sub
Because you have the THEN action in line 4 on the same line as the IF, you don't need the End If on line 5. So you need to do either
Expand|Select|Wrap|Line Numbers
  1. If .Cell(r, 1).Range.Text <> "W5" Then
  2.     colorit = Not colorit
  3. End If
Or remove line 5.

I would also highly recommend using indentation to make your code easier to read. For example,
Expand|Select|Wrap|Line Numbers
  1. Sub colorrowcellone()
  2.     With aTable
  3.         For r = 2 To .Rows.Count
  4.             If .Cell(r, 1).Range.Text <> "W5" Then 
  5.                 colorit = Not colorit
  6.             End If
  7.  
  8.             'then determine color to use on this row, light green or no shading
  9.             If colorit Then
  10.                 Color = RBG(255, 255, 255)
  11.             Else
  12.                 Color = RBG(220, 230, 241)
  13.             End If
  14.  
  15.             .Rows(r).Shading.BackgroundPatternColor = Color
  16.         Next r
  17.     End With
  18. End Sub
Jan 25 '19 #2

Post your reply

Sign in to post your reply or Sign up for a free account.