
Investigating VBA code
In this recipe, we will inspect a short VBA subroutine. Like all programming languages, VBA has a specific syntax, and the best way to understand the principles is to see what the coding looks like.
What we're going to see here will lay the foundation for the other recipes in this book, so pay close attention.
Getting ready
In order to investigate and edit VBA code in Excel, or any of the other Office applications, we first need to activate the Developer toolbar. Here are the steps:
- Open MS Excel and select Blank workbook from the opening screen.
- Open the 01_VBA_Code.xlsm sample file. Click on [Enable Content] on the SECURITY WARNING ribbon.
- If the Developer ribbon is not visible, activate the Backstage View by clicking on File, which will display the following screen:
Figure 1.1 – The Backstage View
- From the category list on the left, select the last option, Options. The Excel Options dialog box appears:
Figure 1.2 – The Excel Options dialog box
- From the categories on the left, select Customize Ribbon.
- To the far right, under the Main Tabs heading, look for the Developer option. Select the checkbox.
- Click on OK to accept the change. The dialog box will close, and Excel will now display the Developer tab.
How to do it…
With the Developer tab activated, we will now proceed with the steps for this recipe:
- With 01_VBA_Code.xlsm open, click on the Developer tab:
Figure 1.3 – The Developer tab
- In the Code group (the first group on the left of the ribbon), select the Macros icon. The Macro dialog box opens:
Figure 1.4 – The Macro dialog box
- The VBA code we want to investigate is contained in the only macro: Area_Bold. Click on the Edit button. The Microsoft Visual Basic for Applications window will open. Maximize the window, if necessary:
Figure 1.5 – The Microsoft Visual Basic for Applications editor window
- In the code window (the large area on the right), a short VBA subroutine is displayed.
How it works…
The subroutine in the VBA Editor looks like this:

Figure 1.6 – The subroutine in the VBA Editor
Let's try to understand this subroutine. Any and all subroutines in VBA start with the Sub keyword, followed by the name of the subroutine (macro), and end with two brackets.
A single apostrophe allows you to enter a note. Comments can be added anywhere in a subroutine and will be displayed in green text, as long as it is on its own line or after a line of code (that is, it cannot be before a line of code as it would obviously comment out the code too). Indented lines without apostrophes are VBA instructions. In this case, the first instruction is when the range B1 to E1 is selected.
The selection's font style is then set to Bold. The last instruction moves the focus to cell A1. Finally, the subroutine is concluded with the End Sub keywords.
There's more…
Whether you record a macro or manually type the coding, you will always find this basic syntax structure in VBA.
In future chapters, we will be working with much longer subroutines. Before we get there, though, we are going to record a macro. With your newly acquired knowledge, you will be able to investigate the VBA code for that too.