VBA - Installation and First Program


For these example we are going to use Microsoft excel and the VBA environment contained. VBA is also contained in other Microsoft products and can be used for these - for example this will work in Microsoft Word. To access these feature we need to have access to the "Developer" tab on the ribbon. As default this is turned off. To turn it on select file on the excel ribbon.

Once file is selected the options menu can be opened.

Inside the options menu there is a tab to "Customize Ribbon".

Putting a tick in the "Developer" turns this on the ribbon.

Now we can select this tab on the ribbon and view the new tools at out disposal. Select "Visual Basic" This will open a new window, the Visual Basic environment.  

There are a lot of new button here, for now we are going to create a new module. A module a container for code to be stored. It is possible to create code in sheets but for now these example we are going to use modules.

Enter the following three lines : 

Sub first_program()

MsgBox "Hello World!"

End Sub

We will cover what each line is doing in the below. Pressing the play button on the top menu will run the code while the cursor is inside the code block (as shown above). Also the F5 key will execute the code.

Finally a little popup will appear and reward you for your hard work. If this worked - Well done for getting this far and get your self on to the next tutorial. If not please check the steps above carefully.

Sub first_program()

MsgBox "Hello World!"

End Sub

Line 1

"Sub" - All program start with these three letters. This is reserved word and cannot be used for varaibles

"first_program" - This is the programs name. This must begin with a letter and can only contain letters , numbers and underscore.

"()" - Brackets are included at the end of the line. If we were to pass values to this program these would be included in the brackets. For this example we are passing nothing to the program.

Line 2

"MsgBox" - This is an internal fucntion that created a popup to the user. This required soemthing to follow that will be included in the box.

"Hello World!" - This is a string of characters that are to be included in the message box.  Note that strigns are contained in quotation marks.

Line 3

"End Sub" - This indicated the end of the program.

Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.

Must Read Articles

VBA and Microsoft Excel - Getting the most out of excel with VBA programming
VBA and Microsoft Word - Getting the most out of word with VBA programming
Application Object - Using the applications features in your code.
Installing VBA and First Program - What do you need to start using visual basic application VBA.