Module 1 - Getting Started in VBA
Lesson 1.1 - Your First Program
Topic 1.1.2 - Writing a Basic Procedure

Now that you've set up the Visual Basic Editor and created a module you can finally get on with writing code!  As a simple first example we'll create a procedure which creates a new worksheet and enters some details into it.

Files Needed

You don't need any files for this page.

Completed Code

You can click here to download a file containing the finished code.

As a quick checklist before you start writing code, in the previous part of this lesson you should have already:

  1. Opened Excel and created a new workbook.
  2. Opened the VBE by choosing Developer | Visual Basic from the Excel ribbon or pressing Alt + F11.
  3. Created a new module in the VBE by right-clicking in the Project Explorer and choosing Insert | Module.

Your screen should resemble the one shown below:

Starting point

This is the starting point for this part of the lesson.

Creating a Subroutine

You create code in VBA by writing a list of instructions that you want your application to carry out.  The instructions you write need to be contained within a procedure.  VBA has several types of procedure but we'll be sticking with the simplest type, called a subroutine.

You can begin a subroutine by writing the word sub followed by the name you want assign to it:

Write sub

You can't use spaces in the names of things in VBA. Here we've used underscores instead, but you can also just omit spaces altogether.

There are several other rules you must follow when assigning names to things in VBA.  You can see a list of these rules in the Reference section of this module.

After entering the name of your subroutine, press Enter to create it.  You should see several things happen:

Create sub

The VBE will add a set of parentheses after your subroutine's name and add the End Sub line for you.

 

Code Layout and Comments

You can now write the instructions of your program between the Sub and End Sub lines.  You may find it helpful to use blank lines and indenting to make your code easier to read.  Press Enter to create a blank line then Tab to indent the cursor one tab space:

New line

You don't need to lay out your code like this, but it does make life easier when you have to read or edit your code later!

 

Before writing an instruction it can be helpful to add a comment which describes what your code is doing.  You can add a comment by typing ' (an apostrophe) followed by any text:

Comment

You don't need to add comments but it will help to remind you what you did when you read your code later.

 

Complete the comment by pressing Enter at the end of the line:

Comment

Comments are displayed with a green font colour in the VBE.

 

Writing Simple VBA Instructions

Now you're ready to write an instruction that your program will carry out when you choose to run it.  Most instructions in VBA follow a pattern in which you start by referring to the thing you want to manipulate (an object in VBA speak) and then either apply an action (a method) to it or alter an attribute (a property) of it.  You separate the object from the method or property with a full stop (or period).  The basic syntax is shown in the example code below:

'apply a method to an object

Object.Method

 

'change a property of an object

Object.Property = something

In our example, the object we want to manipulate is called Worksheets, and we want to create a new item by using its Add method.  Start by writing the word worksheets at the beginning of the instruction:

Refer to object

It doesn't matter whether you use upper case or lower case letters.

 

Now type a full stop (period) to see a list of things you can do to the object:

Intellisense

Typing a full stop makes the IntelliSense list appear. This list shows the methods and properties of the object you have referenced.

 

You can select an item from the IntelliSense list by either:

  • Typing it out.
  • Double-clicking it with the mouse.
  • Selecting it with the cursor keys and pressing either:
    • Tab to remain on the same line; or
    • Enter to move to the next line.

Whichever technique you choose, press Enter at the end of the line to complete your first instruction:

First instruction

The VBE will capitalise words that it recognises automatically.

 

When we finally run the subroutine, this instruction will create a new worksheet and automatically select it in Excel.

You can attempt to display the IntelliSense list at any stage by pressing Ctrl + Spacebar or Ctrl + J.  Try doing this at the start of an empty line.

Press Enter to create a blank line and then add a new comment as shown below:

New comment

Type an apostrophe followed by some text as shown, then press Enter.

 

For the next instruction we'll change the Value property of a Range object.  To make sure that we modify only one cell we must specify the address of the one we want to change.  Start the instruction by typing the word range followed by an open parenthesis character (.

Range

The tooltip that appears indicates that you can specify which cell you want to reference.

 

Enter the cell reference of the cell you want to change inside a pair of double-quote characters, followed by a close parenthesis.  In our example we'll reference cell A1:

Cell ref

It doesn't matter whether you use upper case or lower case letters here.

 

Next, type a full stop and look for the Value property in the IntelliSense list:

Value property

You can type the entire word, or just part of it before selecting it from the list.

 

Now we need to assign a value to the property.  For this example, we'll enter our name into the cell.  To do this, type an = sign followed by your name enclosed in a pair of double-quotes.  Press Enter at the end of the line:

Complete instruction

Literal text in VBA must be enclosed in double-quotes. Feel free to use your own name!

For the next instruction we'll enter the current date into cell A2.  Start by referring to the Value property of this cell:

Next cell

Type an = sign after referring to the Value property.

Now we want to calculate the current date.  To do this, press Ctrl + Spacebar and look for the Date keyword in the IntelliSense list:

Date

Using the Date keyword means that VBA will calculate the current date at the point the code is executed.

 

Press Enter at the end of the line to complete the instruction:

Two instructions

The completed instruction.

For the final instruction in this subroutine, we'll apply some formatting to the two cells whose values we've changed.  Start by creating a blank line followed by a new comment:

new comment

Remember that blank lines and comments are optional.

 

You can refer to a block of cells by entering two cell references separated by a colon character (:).  We then need to refer to the Interior property of those cells:

Refer cells

Enter this code and select the Interior property from the IntelliSense list.

 

Now we need to refer to the Color property of the interior of the cells:

Color property

UK users should be careful of the spelling of Color!

 

We can now assign a value to the Color property.  Type an = sign, followed by the letters rgb and press Ctrl + Spacebar:

Colour picker

You can pick from a wide range of elaborately-named colours!

Press Enter at the end of the instruction and your first subroutine is finished!

Final routine

The finished subroutine should look something like this.

The next part of this lesson will show you how to save your code before you run it.

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 save the code you have written.

This page has 0 threads Add post