Working with the VBA editor
VBA code is event driven. This means that an event needs to trigger the code to run. Code that runs in response to an event is called an event handler. To create an event handler, select an object from the object box and then select an event from the procedure/event box. Inside the event subroutine, enter the code that should run in response to the event.
You can also create procedures that can be called from event handlers. All procedures must start with a
Sub
or Function
statement and end with End Sub
or End Function
. You can type the Sub
and End Sub
or Function
and End Function
statements directly into the code window or select Procedure
from the Insert
menu to open the Add Procedure
dialog box. Using the Insert/Procedure
options automatically inserts the statements into the code window.The VBA editor provides code editing, navigation keys, and keyboard shortcuts in the code window. It also includes tools that complete keywords as you type (Intellisense), provide a list of options at each stage in building an expression, and verify syntax after you enter a line of code. You can turn these tools on in the
Options
dialog box of the Tools
menu.To display help on the VBA IDE, on the VBA menu click
Help
> Microsoft Visual Basic For Applications Help
, or press F1
in a dialog box.By default, all procedures are Public. This means that they can be called from any procedure in the VBA project they are written in. For example, if you have inserted a VBA module into a VBA project and written procedures in it, as long as they are Public, you can call them from code you have written for the VBA project object. If a procedure is Private, you can only call it from other procedures in the same VBA project object or module.
TIP:
Type a single quote at the beginning of a line to add a comment to code. Everything from the quote to the end of the line is ignored.
Provide Feedback