| Posted at 09:44 AM on December 23, 2008 |
Perhaps the most interesting things in any program are the variables. Variables are like little bundles to information, tied to a name that humans can easily understand (we have difficulty remembering 1245642, but we can easily remember NumberOfBloomers)
Imagine you have a box of apples and people can put apples into the box or take them out. Any number they like, at any time. How many apples are currently in the box?
That?s where a variable comes in. A variable will hold the current number of apples (or any other unit, money, time, number of characters for example), and we can ask the computer for this number at any time during our program. You?d think the computer would get fed up asking for the same number a thousand time, but it never does.
Whole numbers (numbers without a decimal point) are called integers (shortened sometimes to int, just to add to the confusion). We create an integer variable like this?
Dim intBoxOfApples as Integer
To add an apple to the box we would write
intBoxOfApples = intBoxOfApples + 1
and guess what, to take an apple out of the box we would write
intBoxOfApples = intBoxOfApples - 1
So a program to put an apple into a box, and pop up a message to tell us about it, would look like this ?
Sub PutAppleInBox()
Dim intBoxOfApples as Integer
intBoxOfApples = intBoxOfApples + 1
MsgBox ("There is " & intBoxOfApples & " apple in the box")
End Sub
To try it out, swap the Aunt Marys Bloomers code from Lesson 3 and run the code once you have typed it in (and debugged it). The result will look like this ?

Categories: VBA Tutorials (mixed nuts)