Share via


Loops

The following points provide suggestions for ways to save resources when you are executing loops:

  • Analyze your loops to see whether you are repeating memory-intensive operations needlessly. For example, are there any variables that you can set outside the loop, rather than within it? Are you performing a conversion procedure each time through the loop that could be done outside the loop?
  • Consider whether you must loop only until a certain condition is met. If so, you might be able to exit the loop early. For example, suppose you are performing data validation on a string that should not contain numeric characters. If you have a loop that checks each character in a string to determine whether the string contains any numeric characters, you can exit the loop as soon as you find the first numeric character.
  • If you must refer to an element of an array within a loop, create a temporary variable that stores the element's value rather than referring to it within the array. Retrieving values from an array is slower than reading a variable of the same type.

See Also

Optimizing VBA Code | Declaring Variables | Mathematical Operations | String Operations