Types of Procedures
There are three types of procedures.
Command procedures – Command procedures usually contain statements that are the equivalent of options in the ribbon. And other program commands. As such, command procedures will affect their surroundings. For example, a command procedure may open an excel workbook and inserting a heading. They are also the most common type of procedure.
User-defined function procedures – User-defined functions procedures work just like an application’s built-in functions. These procedures accept input values and then manipulate those values and return a result, such as the sum of numbers.
Property procedures – A property procedures is used to set the value of a property of an object that you have created through a module.
When you are naming a command procedure you should consider the following guidelines:
- The name needs to be shorter than 256 characters.
- You are not allowed to include spaces or full stops in the name
- The first character must be a letter or an underscore (_)
Entering VBA statements
On the writing VBA code is fairly straightforward you write the code and hit enter but try to consider the following points and you will be able to write more orderly code.
- Try indenting your code by using the tab button this can make it easier to see and read. Do not indent the sub and end sub, just the code in between.
- You should and a few lines of comments before the procedure to describe what the procedure does, also writing them throughout it can also help you understand it and if you come back to it at a later date you can remember what everything does.
Every time you press enter to start a new line VBA will analyses the statement you just entered and will perform theses 3 tasks.
- It will format the colour of each word in the line. By default, VBA keywords are blue, comments are green, errors are red and all the other text is black
- It converts all VBA keywords to the proper case. For example if you write windowstate VBA will convert it to WindowsState when you press enter.
- Next it will check for any syntax errors and either displays a dialogue box to let you know what is wrong or it doesn't convert a word to its proper case or colour.
You could attend a Excel VBA course which will teach this and more in a lot more detail.