For-Next Loop Optimization

Published on December 7, 2009

The standard syntax for For-Next loops is to repeat the loop counter variable after Next (i.e. Next i). Placing the loop counter variable after Next is not required and unnecessarily slows down your macro. Omit the loop counter variable after Next to boost performance.

Standard For-Next Syntax

For i = iStart To iEnd
        ' Standard For-Next loop syntax has loop counter
        ' (i) after Next
Next i

Optimized For-Next Syntax

For i = iStart To iEnd
        ' Omit the loop counter variable after Next
  ' for better performance
Next

For i = iStart To iEnd
  ' Better yet…
        ' With nested and longer loops add the For statement 
  ' as a comment after Next
Next 	' For i = iStart To iEnd

Tags:
Like this article? Share it!

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Privacy Policy