What Are Variables

A variable is simply a name that is used to refer to an item of data. Each time you want to remember the result of a VBA instruction, think of a name that will represent it. For example, if the number 16,777,216 has to remind you of the total number of cells in a worksheet, you can make up a name such as AllCells, NumOfCells, TotalCells, and so on.

The names of variables can contain characters, numbers, and some punctuation marks, except for the following:

The name of a variable cannot begin with a number or contain a space. If you want the name of the variable to include more than one word, use the underscore (_). Although the name of a variable can contain as many as 254 characters, it's best to use short and simple variable names. Using short names will save you typing time when you need to refer to the variable several times in your Visual Basic procedure. Visual Basic doesn't care whether you use uppercase or lowercase letters in variable names. However, most programmers use lowercase letters in variable names, and when the variable names are comprised of one or more words, they use the title case. That is, they capitalize each word, as in the following: NumOfCells, First_Name.

0 0

Post a comment