Microsoft training courses | Wise Owl - home page

Phone (01457) 858877 or email

INTRODUCTION TO VISUAL BASIC FOR APPLICATIONS (VBA)

Part one of a five-part series of blogs

If you've heard people in your office talking about macros or VBA but don't understand what they mean, this blog series will make everything clear. It's an introduction to the most basic skills you'll need to start creating your own programs in any of the Microsoft Office applications.

  1. Introduction to VBA (Visual Basic for Applications) (this article)
  2. Creating Your First VBA Program
  3. Writing VBA Code (Visual Basic for Applciations)
  4. Running a Subroutine in VBA macros
  5. Problems When Running VBA Code
Posted by Andrew Gould on 01 July 2011 | 16 comments

Introduction to VBA

Visual Basic for Applications, or VBA for short, is a powerful programming language that is embedded in every Microsoft Office product.  Whether you are using Word, Excel or PowerPoint, you are only ever a few mouse clicks away from starting to write your own programs.

Because we use VBA in so many of our blogs, we thought it would be useful to have an article that introduces the uninitiated to the most fundamental techniques you'll need to start working with VBA.  So even if you've never written a line of code, or recorded a single macro, fire up your favourite Office application and read on to take your first steps on the way to becoming a VBA programmer!

Getting to the Visual Basic Editor

Whichever Microsoft Office application you happen to be using, you can be sure that embedded within it is another application called the Visual Basic Editor.  This is the application that you use to write your VBA code.  There are various menu or Ribbon options that will take you to the VB Editor, depending on which application and which version of Office you are using, but you can always get to the VB Editor with a keyboard shortcut.  To do this, hold down the ALT key on the keyboard, and then press F11.

The Visual Basic Editor

This is how the VB Editor should look the very first time you open it.

In the above diagram, the two windows that we've highlighted are:

1. The Project Explorer

2. The Properties window

The VB Editor application is the same regardless of which Office application you are in when you open it.  You will see slightly different things in the Project Explorer and Properties window depending on which application you are using - the diagram above is using Excel 2007.

You can use the shortcut ALT + F11 at any time to toggle between the VB Editor and the Office application you are working in.  Much quicker than clicking with the mouse!

If you can't see the two windows that we've highlighted in the image above, you can go to the View menu at the top of the VB Editor to display them.

Displaying the Project Explorer and Properties window

You can click the options shown here to show the two relevant windows, or use the keyboard shortcuts that are listed next to the options in the menu.

 

Working with Projects

A project is the name for the collection of VBA objects that are part of the file you are working on.  A project is created automatically when you open a new Office file (such as an Excel workbook, Word document, or PowerPoint presentation), and you can only have one project associated with one file.

The only useful thing you can really do with a project at this point is to rename it.  To do this:

Renaming a project

1. Click on the project in the Project Explorer.

2. Type in a new name for the project in the Properties window.

A project name can't contain spaces and various other punctuation characters - it's best to stick to text and numbers.

 

Although you can only have one VBA project for each Office file, you might also see other projects listed when you go into the VB Editor.  These extra projects include things like the Excel Personal Macro Workbook, the Word Normal template, Excel Add-Ins, and the Project Global template.

The Project Explorer in Microsoft Projectg The Project Explorer in Microsoft Word
The VB Editor in Microsoft Project always contains the ProjectGlobal VBA project.  This project is attached to the Global.MPT template file. In Microsoft Word you will always see a VBA project associated with the template your document is based on - usually this will be the Normal template.

Working with Modules

Before you can start writing any code, you need to insert a module.  A module is the electronic equivalent of a blank piece of paper.  Each project can contain multiple modules - the exact number you have will depend on the size and complexity of the system you are developing, as well as your personal preference on how to organise your code.

To insert a new module, right-click on the VBA project you want to insert the module into, and from the menu choose: Insert -> Module

Inserting a module

You can actually right-click anywhere in the project you are working on to do this, but it's easier to spot the bold text of the project name when you have several files open.

When you've done this, you'll see a new item in the Project Explorer window, and the great white expanse of the module you've just created taking up most of the right hand side of your screen.

Renaming a module

You can rename a module in the same way you renamed a project earlier: select the module in the Project Explorer, and type in a new name in the Properties window.  Again, you can't use spaces or most of the punctuation characters - stick to text and numbers to be safe!

Now that you've inserted and renamed a module, you can click into it with the mouse and start typing your VBA code.  Read the next part of this series to find out exactly what you need to type!

INTRODUCTION TO VISUAL BASIC FOR APPLICATIONS (VBA)

Part one of a five-part series of blogs

If you've heard people in your office talking about macros or VBA but don't understand what they mean, this blog series will make everything clear. It's an introduction to the most basic skills you'll need to start creating your own programs in any of the Microsoft Office applications.

  1. Introduction to VBA (Visual Basic for Applications) (this article)
  2. Creating Your First VBA Program
  3. Writing VBA Code (Visual Basic for Applciations)
  4. Running a Subroutine in VBA macros
  5. Problems When Running VBA Code

Comments on this blog

This blog has 16 comments:

Comment added by Mark on 01 February 2012 at 17:57 GMT

This is the best source I’ve seen on the nuts and bolts of VBA.  It definitely cleared up a lot of confusion for me.

Thank you very much

 
Comment added by rif71 on 15 March 2012 at 16:22 GMT

Thanks so much for the effort you have put into creating this tutorial, and for sharing it with all !
Very comprehensive and helpful indeed,..  I personally enjoyed very much going through it,..  opened a whole new horizon for extending excel potential for me !
Thanks again for this excellent guide !

 
Comment added by mcorley on 19 March 2012 at 21:07 GMT
So far, so good!
 
Comment added by dave7 on 31 March 2012 at 18:16 GMT
Thanks very much for this VBA tutorial. For weeks I have been struggling with both paid for and free courses, and this one takes the biscuit in terms of clarity and ease!
Reply from Andrew Gould (blog author)
Happy it helped. Good luck!
 
Comment added by X on 17 April 2012 at 11:52 GMT
Can anyone help me?
I think there is a second part in Creating Your First VBA Program where you create a subroutine with a flashing text cursor poised but I can´t find it.
Reply from Andrew Gould (blog author)
Hi there. Do you mean that you can't find how to create the new blank page or module? That's explained at the end of part 1 of the series, but for reference you can go to the menu at the top of the screen and choose Insert > Module. Hope that helps.
 
Comment added by JK on 23 April 2012 at 09:33 GMT
Fantastic VBA training. Just to the point.
 
Comment added by Pedro147 on 21 May 2012 at 06:21 GMT
Thank you very much. I have been looking at many so called tutorials on VBA over the last week or two and while some of them do offer some basic instruction, when you use their code to run user forms etc that you have spent time creating they do not work. Although I have only had a cursory read of your blog it seems well written and I am sure that the code you supply will work. So thank you, you are indeed a wise owl. Pedro147
 
Comment added by Will on 19 June 2012 at 14:22 GMT
Thanks Wise Owl!  You just made my thesis work a million times easier!
Reply from Andrew Gould (blog author)
Excellent!  Make sure we get a credit!
 
Comment added by MarcioRo on 15 August 2012 at 20:34 GMT
I loved the introduction course to Excel-VBA, thank you. Once I finish the basic modules (I guess it finishes with "Problems When Running VBA Code"), what would be the next step/link you advise me to take?
Reply from Andrew Gould (blog author)
Glad you liked it!  A good next step would be to begin working your way through the articles from our full Excel VBA tutorial, which you can find here.  Good luck!
 
Comment added by Aeshie on 01 October 2012 at 08:04 GMT
Great blog! Thanks! 
 
Comment added by code mason on 04 October 2012 at 01:02 GMT
Workbooks.Add

has a context of a new workbook, with three sheets.

range("A1")

has a context of  a particular cell on a particular worksheet (the current worksheet?). The warp jump in context deserves some explanation or code that makes it explicit.
Reply from Andrew Gould (blog author)
Thanks for your comment and I see what you mean but I didn't want this article to get bogged down in that sort of technical detail.  This blog is purely about getting used to working in the VB Editor and making simple things happen - we've saved the detailed explanations for the relevant parts of the full VBA tutorial.
 
Comment added by firstfoot on 12 December 2012 at 18:55 GMT
hey, Thanks for the detail introduction class for VBA. it was informative and easy to understand looking forward for many of such works. You rock
Reply from Andrew Gould (blog author)

Thanks, happy it helped you!

 
Comment added by Banbaji on 14 December 2012 at 15:11 GMT
One thing that might be worth mentioning is CTRL+F8, which executes the code until it reaches the line the cursor is on. I briefly reviewed the tutorial on Debugging in VBA and did not find this command mentioned there either. Just a thought, as I have found this command quite useful (However, I am by no means an expert).
Reply from Andrew Gould (blog author)
It's certainly worth mentioning - thank you for sharing!
 
Comment added by Sandeep on 16 December 2012 at 19:22 GMT
Thanks it really helped me in learning the basics of VBA.
Reply from Andrew Gould (blog author)
Happy it helped and thanks for reading.
 
Comment added by Miky144 on 25 January 2013 at 12:34 GMT

Thanx  a lot. this article helped me a lot.

 

But i did'nt find any discussion about "sub" and "endsub". i would be highly obliged if u could explain that too.

Reply from Andrew Gould (blog author)

Glad you found it useful!

Sub and End Sub are described in Part 2 of this series (you can click the links at the top or bottom of any part of the series to find it).  These two keywords mark the beginning and end of a single subroutine or program.

Hope that helps.

 
Comment added by Kumar on 05 March 2013 at 03:53 GMT
Great Style - Easy to follow - takes all the stress out of picking up the basics .. And fast!

Many thanks
Kumar
Reply from Andrew Gould (blog author)
Glad you found it useful!

All content copyright Wise Owl Business Solutions Ltd 2013. All rights reserved.