Problems with block If then statement

S

stevens

I have an interesting problem. I have some VBA code that contains an "If"
statement. 350 lines of code and comments later there is the closing,
matching "End if" statement. The code in between has two do loops and
numerous "if.....end if" statements, all matched up fine. I keep getting an
"End If block without matching If statement" error message. As I mentioned
before, all Loop statements and all If statements are properly matched with
their ending counterparts. Is there a limitation to how many lines of code
you can have between If then and end if statements? I have looked at this
code til I am blue in the face and cannot find a mismatch. Has anyone had a
similar problem?
Steve
 
D

Douglas J. Steele

You sometimes get that error message for other reasons. For instance, if you
have a With block and don't have an End With, or if you have a Select Case
statement and don't have End Select. (I know I often goof, and put End If
when I mean End WIth or End Select!)
 
V

Van T. Dinh

No, I don't think there is a limit in the number of lines of code between If
and End If. If there is a limit, I think it would be the limit in the
number of lines of code in a module and it would be huge.

This is where code indentation would help.

When I do coding, I tend to type in "If" & "End If" straight away on the
next line before going back to fill the middle part. I do the same with
Loops (For & Do) and With statement. It seems to help me avoiding the
missing beginning or ending components of these statements.
 
Top