03 Writing and Running Code
Small set up
Although we could go ahead and write some VBA code straight away, there is one little setup item we should do beforehand. This only need to be done once unless you want to change it again and is found under the “Tools” menu and “Options…”.
Ensure the “Require Variable Declaration” option is ticked. Although this not necessarily essential for VBA, it is still strongly recommended you do this and the reason for this will be explained later.
So we want to write some VBA code in Excel, but where exactly do we put it? Well we have 2 options. The first is to “attach” the code to something in Excel (such as a worksheet) and the second is to create a blank “canvass” which we write code into. We will be using a blank canvass to start with and in VBA these are called “modules”.
Modules (the blank canvass)
In order to write code we need something to write it in and this item is called a “module”. At the top of the Visual Basic editor screen under the “Insert” menu option is the “Module” option. Select this and the “project view” will change to show the newly added module.
We probably want to change the default name from “Module1” to something a bit more useful so if we click on the module highlight it, the Properties window will update with the module’s properties (of which there is only one option – “(Name)”)
You can directly change the name in the propertiess box by click on the name and typing in a new one. This will also change the name you see in the Project Explorer.