Contents
In this article, you’ll learn more about how to work effectively in the Visual Basic editor using dedicated windows.
Open the Visual Basic Editor
The easiest way to access Visual Basic is to hold Alt and F11. Ready! This tool has appeared in front of you, as shown in the screenshot below. Note that opening the editor does not close the regular spreadsheet.
Visual Basic editor
When working in Visual Basic, you have the ability to interact with multiple windows. Using them greatly simplifies writing code and testing it. To open them, you need to go to the “View” tab, which is located at the top. Now we need to consider all the available options.
Project window
Window “Project” can be found in the VBA editor on the left (this can be seen in the above screenshot). It displays all the VBA modules that are in this Excel document.
Initially “Project” VBA consists of:
- Object “This book”associated with the book.
- Objects “Leaf”, which are associated with all sheets in the document.
You can also add additional objects yourself “User Form”, “Module” и “Class Module” to your project. In the screenshot above, the selected “Module”, and added it called Module1.
To create a new object, do the following:
- В “Project” click on the book to which you want to add the object.
- In the window that appears, select “Insert”, and then specify the required object class.
It should be noted that each represented object has a window: it contains VBA code.
When writing code, you must follow a few rules:
- The code that will be associated with the book must be inscribed in the object “This book”.
- The code to be associated with a specific worksheet must be entered into the object “Leaf”.
- The generic code (which is not related to anything) must be entered in “Module”.
- If you create your own objects, you should put their code in “class module”.
- When adding code for further human use, it is worth working with User Form.
Code window
By double-clicking on a specific object in a tab “Project”, you will open a window “Code”. In the field that appears, you can enter the desired combination. In the screenshot above, there is a window “Code” for an object called Module1.
The VBA editor checks if the code is written correctly. When it finds errors, it highlights them, so you can immediately identify the place with the wrong combination of characters.
Properties window
In this window, you can see all the parameters and properties of the object selected during development. They differ from each other and depend on the specified object type. For example, for objects “Leaf” и “Module” few properties in common because their functions are not similar.
Debug window
This window will help you when writing code. It allows you to debug expressions, as well as execute individual lines of code. To open it, in the tab “View” need to find the item “Debugging”. Alternatively, this can be done by pressing the combination Ctrl and G.
Variables window
To go to the window “Variables” the menu “View” specify the parameter Variables window. Here you will see all the variables in this process. All indicators are displayed in the form of columns in which the names, values and types of each argument are written. When the program starts, all data is updated. This will allow you to fully control the work and identify any inconsistencies in the code.
Tracking window
You may also need this window when debugging code. It displays the value, type, and context of all metrics that have been previously selected by the user.
It can be opened like this: in the tab “View” select “Tracking Window”. In addition, it will automatically appear if you specify the required expression.
To set this expression, follow these simple steps:
- Highlight the given element in the VBA code.
- On the menu “Debugging” select an option “Fast tracking”.
- Click on the button “Add”.
That’s all. It should be noted that there are still a great many options and commands for writing, executing and debugging VBA code. We will certainly talk about them in other lessons!