This article is part of the Visual Tour of Excel’s Coding Dojo series, exploring each of the Excel VBA Editor’s menus, toolbars, and windows. Here, we’ll take a close look at the Excel VBA editor’s Code window.
“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”
Martin Golding
If you wish to skip to any of the other posts in the series, click on any of the topics listed below:
- How to Open the VBA Editor in Excel
- Exploring each VBA Editor Menu in Excel
- Exploring each VBA Editor Toolbar in Excel
- Getting Started with the Project Explorer
- Getting Started with the Properties Window
- Exploring the VBA Editor’s Immediate Window
- Getting Started with the Locals Window
- Getting Started with the Watch Window
Meet your Mat! The VBA Editor
Most great martial artists first had to get intimate with their dojo. Likewise, to master Excel VBA, you’ll have to get comfy loitering in its coding dojo, the VBA Editor. Naturally, the first step is to open the VBA editor.
“The Dojo is the only place you can die and come back to life, so use it.”
kyoshi Larry Mabson (American karateka)
I, for one, know how daunting the Excel VBA editor can appear to beginners – just take a look at the overview below! However, looks can be deceiving; at least that’s true here. I say that because anyone familiar with programming IDEs will tell you that the VBA editor is one of the more user-friendly IDEs out there.
IDE stands for Integrated Development Environment. They are software that consolidates application development tools.
Most developers only use a few of the VBA Editor’s menus, toolbars, and windows. As a result, they miss out on several tools that would make their work less stressful and enhance their productivity.
But you are not most developers, are you? For one thing, you are here, so I take it you would like to max out your VBA programming potential. Brilliant! You are well on your way there.
Code Window – Overview
The aptly named Code window is the editor area reserved for writing, editing, and testing VBA code. Also, it opens to the right of the Project Explorer in the VBA editor, by default. Further, separate Code windows exist for every object in the Project Explorer. Notably, when recording a macro, Excel automatically inserts a module for the recorded macro’s code in the Project Explorer.
The image below shows several different ways of displaying the Code window:
- Pick the object in the Project Explorer and either click the View Code icon atop the explorer (path 1-2) or choose the Code command from the View menu (path 1-3-4);
- Or, right-click the object in the Project Explorer and select the View Code command from the pop-up Context menu (path 1-5);
- Or, double-click the object in the Project Explorer. Note that this does not work for UserForms, it displays the UserForm in a window instead;
- Or, pick the object in the Project Explorer and press the F7 key or the “Alt + V + C” keyboard shortcut.
Code Window – Layout
The image below shows the Code window, the sample code shown displays a Message box (a built-in dialog box) when the Worksheet object is activated. As you can see, the message box is titled, “Your First Macro, Congrats!” and it says, “Hello, world!” to the user.
The following describes several crucial features of the Code window:
Object Box
Displays the selected VBA object’s name. Also, if the object is a UserForm, the Object Box doubles as a dropdown listing of all objects or controls on it.
The dropdown list lets you navigate quickly between UserForms or their controls. Selecting a new object changes the name in the Object Box and the Code window auto-scrolls to that object’s default procedure.
Procedures/Events Box
Displays the selected procedure/event for the VBA object named in the Object Box. Also, it doubles as a dropdown list of all procedures/events linked to the Object Box’s named object.
The dropdown list lets you navigate quickly between the procedures/events of the Object Box’s named object. Also, selecting a new procedure/event auto-scrolls the Code window to that procedure/event.
Coding Area
This is where the code is written, displayed, or edited. By default, it is a blank space on which you’ll type your code. Notably, each procedure/event begins with a Sub statement and ends with an End Sub statement. Moreover, the sample code in the image shows the Code window’s colour-coding feature which helps you in code writing, reading, and editing.
General and Declarations Dropdown Lists
The first item in the Object Box’s dropdown list is General, while the first item in the Procedure/Event Box’s dropdown list is Declarations. Selecting either of these items sends the cursor to the top of the Code window, outside all procedures/events. That’s where code common to all procedures/events in the window is written. Such code includes Option statements, module-level variable declarations, and general procedures.
Margin Indicator Bar
Displays useful indicators that speak to the nature of a line of code (e.g., if the line is a breakpoint).
Full Module View Icon
Displays all the code in the Code window. It shows General and Declarations areas, as well as all procedures/events.
Procedure View Icon
Displays only the selected procedure/event’s code.
Line/Column Indicator
Displays the current position of the cursor within the Code window. The line number and column shown are irrespective of the view option selected.
General procedures are those independent of any events or user/system action. They can only be explicitly invoked (or called) by other procedures.
Splitting the Code Window
Sometimes, you’ll like to have two code segments open and independently scrollable at the same time, in a Code window. The VBA Editor lets you do this with the Split Bar. The Split Bar sits atop the Code window’s vertical scroll bar. The images below show how you can do (and undo) this.
The Long and Short of It
So, there you have it! You now know all you need to know about the VBA editor’s Code window to take it out for a spin. It is the editor area where you’ll write, edit, and test your VBA code. You can also check out the Office Dev Center page for more info on the VBA editor’s Properties window.
As I mentioned earlier, this article is part of the Visual Tour of Excel’s Coding Dojo series exploring each of the Excel VBA Editor’s menus, toolbars, and windows. To check out any of the other posts in the series, click on topic links below:
- How to Open the VBA Editor in Excel
- Exploring each VBA Editor Menu in Excel
- Exploring each VBA Editor Toolbar in Excel
- Getting Started with the Project Explorer
- Getting Started with the Properties Window
- Exploring the VBA Editor’s Immediate Window
- Getting Started with the Locals Window
- Getting Started with the Watch Window