Module 1 - Getting Started in VBA
Lesson 1.1 - Your First Program
Topic 1.1.1 - Preparing to Write Code

Although you're probably itching to jump straight in and start writing code there are a few things you'll need to do first!  This section shows you how to get started in the Visual Basic Editor application.

The VBE

Get used to this view: you'll be spending a lot of time here!

Files Needed

You don't need any files for this page.

Completed Code

There is no example code to download for this section.

Start by opening Excel and creating a blank workbook.  All VBA code must be stored in some form of Microsoft Office document - as this is an Excel VBA course it makes sense to use an Excel workbook!

The Developer Ribbon Tab

Now we need to open the Visual Basic Editor (VBE) - the application in which you'll write all your VBA code.  One way to do this is to use the Developer tab in the Excel ribbon.  If you can't see the Developer tab, you can customise the ribbon to display it:

Customise ribbon

Right-click the name of any visible ribbon tab and choose Customize the Ribbon...

 

You can now choose to display the Developer tab:

View Developer tab

Check the box next to Developer and then click OK.

 

You can now open the VBE by choosing Developer | Visual Basic from the Excel ribbon:

Open VBE

Click the first tool on the Developer tab.

You can also press Alt + F11 on the keyboard to open the VBE.  It's still worth displaying the Developer tab as it contains several other useful tools.

The Visual Basic Editor

When you first open the VBE it should look something like the image below:

VBE

The default appearance of the VBE.

When you want to return to Excel, you can do so using one of several methods:

Menu Keyboard Toolbar
View | Microsoft Excel Alt + F11

The VBE displays two windows by default:

Window name Description
Project Explorer Shows a list of all the open VBA projects and the objects which belong to them.  This appears in the top left area of the VBE.
Properties Window Shows the attributes of the object that is selected in the Project Explorer.  This appears in the bottom left area of the VBE.

If you can't see these two windows you can use the View menu at the top of the VBE to display them:

View menu

Use the View menu to display any windows in the VBE.

 

Creating a Module

Before you can write any VBA code, you'll need to create a container in which to put it.  The basic container for code in VBA is called a module and you can create one as shown in the diagram below:

Insert module

Right-click inside the Project Explorer (here we've right-clicked on the project name) and choose Insert | Module to create a new module.

 

You can also choose Insert | Module from the VBE menu to create a new module.

When you've inserted a module your screen should resemble the one shown below:

Inserted module

The module appears in the Project Explorer and opens in a separate window in the main area of the screen.

You can manipulate the module window using the standard controls in the top right corner of its window:

Control window

You can minimise, maximise and close the module window using the three buttons in its top right corner.

 

If you close a module, you can double-click its name in the Project Explorer to reopen it.

Using Multiple Modules

As you begin to work on larger and more complex projects, you'll find it useful to use multiple modules to organise your code.  You can insert as many modules into a project as you like:

Insert module

You can insert a new module into the project in the same way as you inserted the first.

You can view the code in a module by double-clicking its name in the Project Explorer window:

View module

Double-click a module to see the code it contains. The name at the top of the VBE window indicates which module you are currently viewing.

 

Deleting Modules

You can delete a module from a project by right-clicking its name and choosing to remove it:

Remove module

Right-click the module in the Project Explorer window and choose the Remove option from the menu.

 

You'll be asked if you want to export the module before it is removed from the project:

Export

Click No to delete the module without exporting it.

 

Increasing the Font Size

One last thing you may wish to do before you start writing code is to change the default font size of the VBE.  To do this, choose Tools | Options... from the VBE menu and complete the dialog box as shown below:

Alter font size

Select the Editor Format tab at the top of the dialog box and select a new font size from the drop down list shown. Click OK to confirm your choice.

Congratulations, you're now ready to start writing VBA code!  Start the next part of this lesson to learn how.

You don't have much to practise yet!  It's probably best to move to the next part of this lesson to learn how to write basic VBA code.

This page has 0 threads Add post