What are Excel VBA Variables?
I like to think of an Excel VBA variable as a place to store information. Now, this definition is not particularly exciting! But, thinking about variables in this way can help understand them and maximise their impact in your work.
Let’s explore this definition a little more. You could compare an Excel VBA variable to a cell in a spreadsheet – which is, of course, where we usually store information in Excel. Indeed, you could substitute a VBA variable for a cell in many Excel VBA routines and still get the job done; but, it would not be as slick or efficient. Nevertheless, ‘a place to store information’, a bit like a cell, is a reasonable conceptual starting point.
How do we typically use Excel vba variables in coding? Let’s look at the example of a loop. A loop is a repeated set of instructions that allows us to get mind-boggling amounts of work done. However, they require careful control (all of us have been stuck in an endless loop at some point!) VBA variables allow us to achieve control in a neat-and-tidy way.
We could, theoretically, reference any object in Excel to control a loop. For example, we could say ‘keep running the loop until the value in cell A1 = 10’. This would work perfectly well, if the value in cell A1 incremented up or down towards 10 as the loop ran. The obvious downside is that we would have to update an object in the spreadsheet (in this case, a cell) which diminishes the efficiency of the code. Further, a spreadsheet cell is usually visible to the user, which may not be desirable.
With this example in mind, what advantages does a variable offer? First, it is quick and easy for Excel to work with, because Excel can change the value that is ‘assigned’ to the variable very efficiently. Remember, a variable is a place to store information; it is faster for Excel to store and recall information using a variable, than to interact with the spreadsheet to change a cell value.
Second, a variable is not usually visible to the user. This means variables are easier to control because, most of the time, they are ‘hidden’. It is not that we don’t trust the user! Rather, variables control processes that should not be easily changeable, such as looping through code. Variables offer superior control to the programmer, which helps create robust applications that do not break.
With these obvious advantages, it intrigues me that people tend to shy away from using VBA variables. Why is this? As is often the case with Excel, it is problem of communication. The language of variables is not easy to understand, so people think ‘they must be for computer programmers only!’ String? Integer? Long? What is the point of these terms, apart from confusing and often deterring the layperson? Let’s dig into it…
In a practical sense, variables help Excel organise its memory – where Excel compiles information to display what we see on the screen. In an efficient application, you could compare Excel’s memory to a beautifully designed wardrobe – it has differently sized spaces for different items, making everything easy to store, and to find. This is the point of variables – they tell Excel the kind of information that is coming; Excel then allocates an appropriately sized space to store it. This is why we ‘declare’ variables (‘Dim counter as integer’ for example), to tell Excel the type of information (‘integer’), and to create a name (‘counter’) to refer to the variable.
What are some common variable types? An Integer variable can store whole numbers up to a value of approximately 32,000 and requires a small amount of memory (just two bytes!) A Long variable can store larger whole numbers but requires twice as much memory. Other variables store decimals (Single, Double), text (String), and other data types. You can find comprehensive lists online. The main variable types I use are Integer, Long, Single (for decimals), String (for text) and Boolean (true or false).
When declaring a variable, we must also name it. A simple letter (Dim ‘x’ as integer) would do the job; however, fans of the channel know that I prefer informative variable names. An informative variable name tells the programmer what the variable does. For example, ‘counter’ is an informative variable name because it counts the number of times a loop has run. I use ‘ChrisCell’ when looping through a collection of cells, because the variable (in this case a ‘range’ variable) represents a cell. We can use any name apart from words that Excel reserves for its own use (‘row’, ‘cell’ etc.) An informative variable name requires more typing but allows us to understand what is going on much better; for me this is a worthwhile trade-off.
So, why not try out some VBA variables? You can find examples of all the above variables in our tutorial videos. They are central to the cool macros we create. Developing basic confidence in their use should lead to slicker and more robust Excel implementations. Good luck and let us know how you get on!