Welcome to our VBA tutorial!
The lesson is divided into the following sections:
Changing macro security settings
If you want to create and run macros, first of all you have to change macro security settings.
By default, all macros in MS Office are disabled. This is because macros can make many things, also destructive, including removing data from your computer.
To change these settings in MS Excel 2003 you have to click the following buttons:
The window shown below will pop up:
As you can see there are four levels of macro security:
- Very High
If your macro security level is set to very high only macros installed in trusted location will
be allowed to run.
- High
You can only run macros signed as trusted by this security level.
Since you will create and run lots of macros during this course, it would be
waste of time signing all of them - so this option and previous one are out of question.
- Medium
When you open a file that contains any macro, the warning message box will appear on your screen.
This is the most suitable option - every time you can decide by yourself if you want to run or block macros in
the file you are about to open.
- Low
When you select this security level, all the macros will be allowed to run without your permission or any warning.
This option is not recommended, because you can run into a file with macros causing serious damages to your computer
without even knowing it.
If your macro security level is set to very high only macros installed in trusted location will be allowed to run.
You can only run macros signed as trusted by this security level.
Since you will create and run lots of macros during this course, it would be waste of time signing all of them - so this option and previous one are out of question.
When you open a file that contains any macro, the warning message box will appear on your screen.
This is the most suitable option - every time you can decide by yourself if you want to run or block macros in the file you are about to open.
When you select this security level, all the macros will be allowed to run without your permission or any warning.
This option is not recommended, because you can run into a file with macros causing serious damages to your computer without even knowing it.
If you use Excel 2007, there are two ways to show and change macro security options.
The first way is to click the Office logo in the upper-left and then Excel Options button (both of them are bordered
in red in the picture below).
The window like in the picture below will appear on your screen. First you should select Trust Center from the list on
the left side and then click Trust Center Settings... in the lower right corner (marked in red).
The Trust Center window will pop up. Select Macro Settings from the list on the left side:
The window above can also be displayed by clicking the proper icon in the Developer tab on Excel ribbon.
In order to do that first you have to show
Developer tab on Excel 2007 ribbon and then go to this tab and click Macro security button (marked in red).
Available security levels are listed in the right side of the Trust Center window (marked in green):
- Disable all macros without notification
If you select this option Excel will not allow any macro to run, even if it is digitally signed. Obviously,
this option is out of question when you learn VBA, since you will create and run lots of macros.
- Disable all macros with notification
Description of this option can be a little bit tricky, because it suggests that you will not be able to run any macro.
In fact, if you select this option Excel will ask you whether to enable or disable the macros every time it detects any
macro in the file being opened.
It is the most suitable option, because you can decide by yourself whether to run macros or not.
Depending on the type of file being opened, two kinds of messages can be displayed on the screen.
When you open a file in *.xlsm format (i.e. Excel Macro-Enabled Workbook), the file is being opened normally,
but in the part between Excel ribbon (toolbar) and formula bar the warning about macro presence appears:
In this case all macros are disabled by default. If you want to use them, you have to click Options... button
and select Enable this content in the window that pops up on the screen.
For other types of Excel files (Excel 2003 files, Excel Add-In files etc.), before a file containing
macro is opened, the warning like below will pop up on your screen.
- Disable all macros except digitally signed macros
In this case Excel will allow to run only macros with digital signature. Since you will create and run lots of
macros during this course, it would be waste of time signing all of them. However, later in this tutorial you
will see how to create a digital signature for your macros.
- Enable all macros
When you set macro security to this level, Excel will run all the macros in every file without your permission or
any warning.
This option is not recommended, because you can run into a file with macros causing serious damages to your computer
without even knowing it.
If you select this option Excel will not allow any macro to run, even if it is digitally signed. Obviously, this option is out of question when you learn VBA, since you will create and run lots of macros.
Description of this option can be a little bit tricky, because it suggests that you will not be able to run any macro. In fact, if you select this option Excel will ask you whether to enable or disable the macros every time it detects any macro in the file being opened.
It is the most suitable option, because you can decide by yourself whether to run macros or not.
Depending on the type of file being opened, two kinds of messages can be displayed on the screen.
When you open a file in *.xlsm format (i.e. Excel Macro-Enabled Workbook), the file is being opened normally,
but in the part between Excel ribbon (toolbar) and formula bar the warning about macro presence appears:
In this case all macros are disabled by default. If you want to use them, you have to click Options... button
and select Enable this content in the window that pops up on the screen.
For other types of Excel files (Excel 2003 files, Excel Add-In files etc.), before a file containing
macro is opened, the warning like below will pop up on your screen.
In this case Excel will allow to run only macros with digital signature. Since you will create and run lots of macros during this course, it would be waste of time signing all of them. However, later in this tutorial you will see how to create a digital signature for your macros.
When you set macro security to this level, Excel will run all the macros in every file without your permission or any warning.
This option is not recommended, because you can run into a file with macros causing serious damages to your computer without even knowing it.
Opening the VBA editor
You can open the VBA editor in a few way:
-
By clicking Alt + F11 shortcut
in Excel's main window
-
By clicking a proper button on the menu bar (since there is no menu bar in
Excel 2007, this way is available only in Excel 2003 and lower)
In order to open the VBA editor straight from the menu bar, you have to click the following buttons:
-
By clicking a proper icon on Excel's toolbar
If you want to open the VBA editor this way, first you have to make Visual Basic toolbar visible.
You can do it by clicking the following buttons on the menu bar:
and ticking Visual Basic (like in the picture below).
Once you do that, the additional toolbar will appear:
The icon bordered in red in the picture below is the one you should use to open the VBA editor.
If you want to show VBA Tab on Excel 2007 ribbon, first you have to open Excel Options window. In order to do that click Office
logo in upper-left and then Excel options button (both of them are marked in red in the picture below).
The Options window will appear on the screen. You have to select Popular from the list on its left side and check
Show Developer tab in the Ribbon as active.
The Developer Tab is presented below. The button to run VBA editor lies on its left side (bordered in red on the
picture).
In order to open the VBA editor straight from the menu bar, you have to click the following buttons: