Starting out with VBA
Opening the VBA Editor
If you are new to VBA, you will need to know where to start. The first thing to do is open the VBA Editor. The easiest way to do this is to start Excel, then press Alt-F11. This will bring up the VBA Editor which is made up of 4 main windows.
- The top left window is the VBAProject window that lists all the VBA objects that can contain code.
- In the bottom left is the Properties window. When you select a module, form or other object, all the available properties that can be set will be visible here.
- In the bottom right is the Watches window. When you debug VBA code, you can “watch” the value of variables and other items so that you can step through the code and see how these variables change throughout code execution.
- The top right window is the main code window. This is where you place any VBA code that you write.
Adding a new Module
To get started, you will need to add a new Modue to your workbook.
- First of all, you should select the VBA Project that refers to the workbook where you want to put your VBA code. In this example, it is Book1.
- Once you have selected the correct workbook, then click the drop down arrow next to the “insert object” button. From here you can choose what type of object to insert. In this case, you should insert a “module”.
Adding VBA Code
Once you have your module added to your workbook, you can add some VBA code into the code window.
- If you have just added the module, then the right hand VBA code window will already display the code page. Otherwise, you can double click on the module object in the VBAProject window in order to display the correct VBA code window on the right. Be careful – if you only single click on the VBAProject on the left, then the window on the right may not display the correct module on the right. You have to double click to make the associated code window appear.
- Once you have the correct code window on the right, you can add your VBA code. Try typing in “sub SampleSub()” into the code window. You will notice that VBA automatically adds “End Sub” to your code, and places a blue line to separate your new code from any other code you may have.