To create a spreadsheet with functionality beyond the defaults, you write code. Microsoft Visual Basic is a programming environment that uses a computer language. In the previous lesson, we saw that that language is called Visual Basic for Applications (VBA). Although VBA is a language of its own, it is in a reality derived from the big Visual Basic computer language developed by Microsoft. In our lessons, we will learn how to use VBA in Microsoft Excel.
To take advantage of the functionalities of the Microsoft Visual Basic environment, there are many suggestions you can use or should follow. Because VBA is normal computer language, there are various rules you must follow for the language to work.
In our lessons, we will use the word VBA sometimes but most of the time, we use the expression "Visual Basic Language". When we use "Visual Basic language", we refer to a concept that is recognized by all child languages of Visual Basic, including VBScript and VBA. When we will use the word VBA, we refer to a concept that either is proper to VBA as a language and is not necessarily applied to some other flavors of Visual Basic, or to the way the Visual Basic language is used in Microsoft Excel. For example, the word String is used in all Visual Basic languages but the word Variant is not used in the Visual Basic language version used in the Microsoft Visual Basic (2008) programming environment.
In the previous lesson, we saw different ways you could launch Microsoft Visual Basic. One of the techniques consisted of creating a macro. That's the technique we will use in our early lessons.
To create a macro, on the Ribbon:
In each case, the Record Macro dialog box would come up:
From there, accept or enter a name for the macro. As an option, you can type a description of the macro in the bottom text box. Once you are ready, click OK. This would bring you to the document in Microsoft Excel where you can do what you want. After doing what is necessary, to end the creation of the macro, on the Ribbon:
When you create a macro, skeleton code is generated for you. To access the code generated for a macro, on the Ribbon:
Any of these actions would open the Macros dialog box that would display the list of macros in the current document:
To see the code of a macro, click its name and click Edit.
We will try to reduce as much as possible the code that will be written for you. Still, there are some lines and words we will keep or use but will ignore them for now. As we move on in our lessons, you will understand what everyone of those words means. The code generated in the above Practical Learning section was:
Sub Exercise1() ActiveCell.FormulaR1C1 = "=2" End Sub
The first line of code has the word Sub. We will introduce it later on. Exercise1 is the name of the macro we created. We will come back to names in a few sections in this lesson. We will also come back to the role of parentheses. The section of code ends with the End Sub line. We will come back to it when we study the procedures. For now, consider the Sub Exercise1() and End Sub lines as the minimum requirements we need as this time, that we don't need to be concerned with, but whose roles we can simply ignore at this time.
The most important line of our code, and the only line we are concerned with, is:
ActiveCell.FormulaR1C1 = "=2"
This line has three main sections: ActiveCell.FormulaR1C1, =, and "=2". For now, understand that the ActiveCell.FormulaR1C1 expression means "whatever box is selected in the document".
The = sign is called the assignment operator. As its name indicates, the assignment operator is used to assign something to another, to give a value to something, or more precisely to store something somewhere.
The thing on the right side of = is called a value. Therefore, "=2" is a value. Based on this, the expression ActiveCell.FormulaR1C1 = "=2" means "Assign the thing on the right side of = to the thing on the left side of =." Another way to put it is, "Store the value on the right side of the assignment operator to the selected box on the left side of the assignment operator." For now, until indicated otherwise, consider that that's what that line of code means.
After creating a macro, you can use it to see its result. This is also referred to as executing a macro or running a macro.
To execute a macro, on the Ribbon:
In the Macro dialog box, click the name of the macro and click Run.
Indentation is a technique that allows you to write easily readable code. It consists of visually showing the beginning and end of a section of code. Indentation consists of moving code to the right side.
The easiest and most common way to apply indentation consists of pressing Tab before typing your code. By default, one indentation, done when pressing Tab, corresponds to 4 characters. This can be automatically set using the Tab Width text box of the Editor property page in the Options dialog box. To change it, on the main menu of Microsoft Visual Basic, you can click Tools -> Options and click the Editor tab:
If you don't want the pressing of Tab to be equivalent to 4 characters, change the value of the Tab Width text box to a reasonable value and click OK. Otherwise, it is (strongly) suggested that you keep to its default of 4 characters.
A comment is a piece of text in code that would not be considered when reading your code. As such, a comment can be written any way you want.
In the Visual Basic language, the line that contains a comment can start with a single quote. Here is an example:
' This line will not be considered as part of the code
Alternatively, you can start a comment with the Rem keyword. Anything on the right side of rem, Rem, or REM would not be read. Here is an example:
' This line will not be considered as part of the code Rem I can write anything I want on this line
Comments are very useful and you are strongly suggested to use them regularly.
The code that was generated in our Practical Learning section contains a few lines of comment:
Sub Exercise1() ' ' Exercise1 Macro ' ' ActiveCell.FormulaR1C1 = "=2" End Sub