This post may contain affiliate links. Please read my disclosure for more info.
Advertisement:

1.2 Using the Visual Basic Editor

Beginning VBA

Beginning VBA

Previous Part: 1.1 Enable the Developer Ribbon

 

The Visual Basic Editor (or VBE as it can be known) is the place where VBA code is entered, or where the code for a recorded Macro is placed.  You can find the Visual Basic Editor within the Developer Ribbon

Developer -> Visual Basic

Or you could use the keyboard; press ALT+F11 (the + indicates that you should hold down the ALT key, press F11, then release the ALT key).

 

The Visual Basic Editor Window


Advertisement:

The Visual Basic Editor will appear.  On the top left, you will see a list of items similar to the following.

selectworkbook

If you double-click on any one of these items a window will open on the right.  This window is where you can type your own code or paste code from other locations.

Do not worry about all the commands and options in the Visual Basic Editor for the moment.  We will go through all of the most important features in the coming chapters.

Note: If you need to create a Module, Form or Class Module (more on these in future sections), then right-click on any of the Sheet numbers or ThisWorkbook and select the relevant option.


Advertisement:

 

Running the VBA code

If you are copying in specific code then you can now copy it in.  If you do not have any specific code then you can copy and paste the following:

Sub MyFirstMacro()
MsgBox "Hello new world!"
End Sub

This is the one time I will allow you to break the 1 golden rule about learning VBA.

There are a number of ways to run VBA code.  You can run code from Excel, however, for the time being, we will just look at how we code from the Visual Basic Editor.   First, click anywhere in the code of the Macro, choose one of the following options:

  1. Select Run -> Run Sub/Userform from the menu at the top of the Visual Basic Editor
  2. Using the keyboard you can press ALT+F5 (remembering to hold down ALT whilst pressing F5)
  3. Using the play button at the top

The code you have entered will now execute.

Hello New World


Advertisement:

That’s it!  You’ve done it.  See, it wasn’t so scary after all, was it?

 

Previous Part: 1.1 Enable the Developer Ribbon

Next Part: 1.3 Recording Macros

Save

Save