Getting started with Excel VBA
Article Index
Getting started with Excel VBA
Running the macro
Excel example

Banner

Step Three - The beginning and the end

Now we have to enter our first simple program.

All programs in VBA have to start and end in a particular way. The first line has to read

Sub name()

where name is the name you want to give your program. Sub is short for Subroutine but more of this later.

For example in our case the program would start

Sub hello()

and the program would be called hello. The final line of the program has to be

End Sub

and leaving this off makes Excel think you haven’t finished your program.

If you enter these lines you will notice that the Sub and End Sub automatically appear in blue and with the correct capitalisation. This is because they are VBA keywords - a fixed set of commands that VBA recognise. To show that it has detected a key word it turns the word blue.

hello

The word hello on the other hand isn’t a VBA keyword because it's up to you what you call a program.

You may also be surprised to find that VBA adds the brackets at the end of Sub Hello if you leave them off - they just have to be there! Similarly it automatically adds the End Sub once again it is essential - you can't have a Sub without and End Sub so the editor adds them for you.

This is a general behaviour and the editor will try to help you as much as possible.

This is great - except when it goes wrong. For example if you don't notice that the editor has added End Sub and you go and add it again. Once you understand the editor you will find that you can work with it rather than against it.

OK so now we have the first and last line of our program and a name

Step Four - Getting the message

Now we have the start and ending lines of a valid VBA program the only problem is - it doesn’t do anything.

The key idea is that a program is read as if it was a list of instructions to be obeyed - which is of course exactly what it is. So what we need is an instruction that lets us display something on the screen.

The simplest such instruction is

MsgBox “message

MsgBox is short for “Message Box” and whatever text you type between the quotes - which have to be included in the instruction - will be displayed in a message box.

So now type, in between the first and last line of your program the command

MsgBox “Hello World - Excel VBA here”

(including the quotes around the message).

So now our entire program is

Sub hello()
 MsgBox "Hello World - Excel VBA here"
End Sub

program

Step Five - Running the program

Now the time has come to try your first program out.

 

To run the program you can simply click on the green “play” arrow in the Visual Basic toolbar. If you can’t see the VBA Toolbar then use the command View,Toolbars and select the Visual Basic option. It should be on by default but some one might have turned it off.

Alternatively you can use the command Run,Start or just press F5 - yes there are at least three different ways to run a program!

Next you should see a dialog box that gives you the choice of which program to run - in general a module can contain more than one program. In this case simply select the hello program and click the Run button.

run

As long as you have entered the three lines correctly you should see the message box displaying “Hello World - Excel VBA here”. You will also notice that the message box has an OK button and a title bar. You have to click the OK button to dismiss the message box and let the program continue - or in our case come to an end.

If you would like to see that the program would indeed carry on after the message box change it to read

Sub hello()
MsgBox "Hello World"
MsgBox "- Excel VBA here"
End Sub

Now when you click on the OK button in the Hello World message box you will see a second message box.

Banner

<ASIN:0470044012>

<ASIN:0470279591>

<ASIN:0470475358>

<ASIN:0471499226>

<ASIN:0596003595>

<ASIN:0495106836>

<ASIN:1840782714>

<ASIN:075066097X>



 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.