Object Variables Save Execution Time

The main reason that objec t variables are important is not to improve readability, but to save execution time. In particular, to execute each of the five lines in the first version of the previous code, VBA needs to resolve the references to the various Excel objects ActiveSheet, Rows(1), and Font. That is, VBA needs to "climb down" the Excel object model. This takes time.

However, in the code that uses an object variable of type Font, VBA only needs to resolve these references once. Therefore, the second version runs much more quickly. This difference can be very noticeable when there are hundreds or thousands of references to resolve.

0 0

Post a comment