473,320 Members | 2,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

End if without block if error - Word VBA macro

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

1 1597
Seth Schrock
2,965 Expert 2GB
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

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

Similar topics

1
by: Tom | last post by:
Suppose you have code structure like this: Option Explicit Dim MyVariable As Single Private Sub CallingProcedure() Call CalledProcedure() ....Do This ... End Sub Private Sub...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
3
by: JPARKER | last post by:
Our DB2 8.2 FixPak9 database running on Win2K crashed with after reporting this message SQL0902C A system error (reason code = 9") ocurred I have searched the DB2 documentation as well as the...
3
by: Daylor | last post by:
hi. i want to destroy the application when i catch some exception. i have multithread app. how can i destroy the application and exit immedietly ?
0
by: lambu76 | last post by:
Hi all, I've this strange problem. I've deployed a big and complicated ASP web application on different web server, all of them installed with Win 2000 Advanced Server SP3. I'm completly sure...
2
by: globomike | last post by:
Hi, can anybody tell me what the reason code 18 means in combination with a SQL0902 error? It would be helpful to know details about the reason code but I could not find any details about that...
8
by: pelicanstuff | last post by:
Hi - Was wondering if anybody could tell me why this rather crappy code is giving me an 'Else without If' error on compile? All the Elses and Ifs look ok to me but there's a few. Private Sub...
3
by: madhavi patel | last post by:
what should i do id complie error shows endif without block if?
8
by: devarts | last post by:
I'm getting an "End if without Block if" error with the following code: Private Sub Command42_Click() Dim ctl As Control Dim strTag As String Dim intCtr As Integer Dim strBuild As String Do...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.