Declaring and assigning variables

Each variable has a name - something that is decided by the programmer. The programmer also decide what type of data it will hold. For example, if a variable is to store a person's age, a whole number - or integer - might be appropriate, as people normally give their age as an integer, such as 45 or 38.

Each variable is of a particular type. The type is designated by the programmer in a process known as declaration. A variable can be given a value - this is called assignment - and that value can be retrieved later. It may be helpful to picture a variable as a tiny box inside the computer as shown here.







The diagram shows that the variable named age has been declared as an Integer data type, and the variable currently contains the value 48.

One can also think of a variable as a spreadsheet cell that is not visible to the user. After all, a cell can contain a data item that can be varied. However, using variables, rather than cells, allows VBA programs to run faster, for VBA can manipulate variables more efficiently. Moreover, variables can be used to hold (or save) temporary data when a macro is executing.

Variables are normally declared before the executable statements of a VBA program begin. We will look at the way in which variables can be assigned values a little later.

Was this article helpful?

0 0

Post a comment