When I first started out with VBA, I had no clue what to do. If that sounds like you, or if you need to do some revision, then this is the place for you.
Table of Contents
The Hello World of VBAHello World
This is the hello world of VBA for Excel. This tutorial will take you through step by step what you need to do to get started in VBA for Excel.
The first thing you need to do is open a new workbook. You should start out with something like this.
Now open the VBA editor by pressing Alt-F11. You should now see this screen
Make sure that you have selected the book1.xlsx module by clicking on VBAProject(Book1) or one of the sheet modules in book1.xlsx. Then click on the insert new module button.
Right, now you have your first blank VBA module, ready to accept your code. Now you need to enter some code into the new module. Enter the code in the right hand pane as you see in the image below.
You can copy and paste from the text below if you want to, however it is worth typing it in yourself.
Sub Hello_World()
Dim myResponse As Integer
myResponse = MsgBox("hello world", vbOKOnly)
End Sub
What you will notice if you type it in yourself, is that after you type in the '()' on the first line, the 'End Sub' will be automatically added. Then when you type in the the second line, type it in exactly as follows (including capitalisation)
dim myResponse as integer
In other words, make the 'R' a capital letter and keep all other letters lower case.
There is a really good reason for doing this. Excel will automatically capitalise the words that are functions or statements, but it will not capitalise words that it doesn't 'know'. Excel knows the words 'Dim' and 'Integer' because they are words that perform a function in VBA, so these will automatically capitalise.
The line 'Dim myResponse As Integer' tells Excel that the word myResponse is a variable of type 'integer'. The next time you type the word 'myresponse' with all lowercase letters, Excel will automatically capitalise the 'R' for you.
I always put a few capital letters in my variable names in the Dim statement and then always type them all lowercase when I use them in my code. That way, if they don't automatically recapitalise, I know I have not used a valid variable. The reason I don't capitalise the first letter is so that I can easily pick out my variables from other keywords in Excel.
Now type in the rest of your code, using all lowercase letters. If the text doesn't capitalise like shown in the image, you have done something wrong, so go back and check.
Right, you are on a roll. Now what you need to do is run your macro. To do this, click on the ribbom View-Macros (Excel 2007) or the menu TOOLS\MACROS (Excel 2003). You will see the following dialog box.
Click run to run the macro. You should see the message box pop up like this.
That's it. Click OK to dismiss the box. Now to see what happens line by line, you need to enter debug mode. Click Alt-F11 again to open the VBA editor. You need to select the VBA Module and click in the margin to set a break point.
Now go back to the spreadsheet and run it again. Alternatively you can press F5 when in the module to start the macro. When you run the module this time, it will go into debug mode. You will be presented with the module code, and each line will be highlighted in yellow as it is executed. You have to hit the F8 key to advance the code line by line.
You can add some extra lines to your code to try out some other things. Try extending your code like shown below. Keep the break point where it is.
Sub Hello_World()
Dim myResponse As Integer
While myResponse<>=2
myResponse = MsgBox("hello world", vbYesNoCancel)
wend
End Sub
Now when you run the code, it will go into a loop until the variable myResponse is equal to the value 2. Place your mouse pointer over the variable myResponse (in the VBA window) after each time you press a different button on the message box. You will see that that value of the variable changes for each of the different buttons you press in the dialog box.