Monday, January 24, 2011

Why even bother declaring variables?

It may seem a bit of a hassle to declare a variable when you could just as easily reference that value (or cell containing that value) directly, right?

While there is nothing that says you cannot, you may find that as your code increases in length, it becomes ever more difficult to recall what value was in cell B1 or D4.  By creating a variable you can now add a name to it such as MyValue and you will find it far easier to follow and edit your code now (and especially down the road).

Whenever you use a value more than once without declaring it as a variable you are forcing Excel to look up that value over and over and over again.  Obviously, this takes time.  The more loops there are?  The more that time ads up.  Variables remain in the computer's memory for use whenever you need it!

Here is a great article on the many variable types available and further background on this topic!

No comments: