devxlogo

Finding Corresponding EndIf

Finding Corresponding EndIf

Question:
I’ve got ten pages of code and there’s an “If” on the first page, a bunch of “If…EndIf” statements throughout the code and the “EndIf” that corresponds with the first “If” lurks somewhere below. Short of just looking for the last “EndIf” (which would work in this case), I’d like to know how to find *any* block closing statement. That is, what’s the trick to finding a corresponding “EndIf”, “Wend”, etc. If the built-in module editor won’t cut it I’m open to suggestions for other editors.

Answer:
Unfortunately, there is no easy way to find the closing EndIf (or otherkeyword) statement. However, good code formatting is often a good solutionfor this problem. For each If/For/other keywords, indent all of the linesbelow it. As soon as you add the opening keyword, put in the closing oneand add a comment indicating which keyword the end one belongs to, as inthis example:

If i > 10 Then   … lots of code …End If  ‘ If i > 10 
This should help you find the ends of your loops. The other option, if itis possible, is to break the logical sections into subroutines, into whichyou pass the necessary variables.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist