2 out of 2 rated this helpful - Rate this topic

Making Faster For...Next Loops

This page is specific to the Visual Basic for Applications (VBA) Language Reference for Office 2010.

Integers use less memory than the Variant data type and are slightly faster to update. However, this difference is only noticeable if you perform many thousands of operations. For example:

Dim CountFaster As Integer    ' First case, use Integer. 
For CountFaster = 0 to 32766     
Next CountFaster 
 
Dim CountSlower As Variant    ' Second case, use Variant. 
For CountSlower = 0 to 32766 
Next CountSlower 

The first case above takes slightly less time to run than the second case. However, if CountFaster exceeds 32,767, an error occurs. To fix this, you can change CountFaster to the Long data type, which accepts a wider range of integers. In general, the smaller the data type, the less time it takes to update. Variants are slightly slower than their equivalent data type.

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.