A workman must first sharpen his tools if he is to do his work well. The VBA editor is the core development tool. It can be said that almost every step in Excel VBA development is carried out in the VBA editor, including writing, debugging, testing, running, and code organization.
This article mainly introduces the basic part of the VBA editor. When reading this article, I suggest you open an Excel workbook and try it yourself according to the content of the article. This is the fastest way to learn VBA.
3 Ways to Open the VBA Editor
1. Use the developer tool tab. Click the Visual Basic command on the tab.
2. Right-click on the Sheet1 workbook and select the option View Code.
3. Use the shortcut Alt+F11, you can get the VBA editor quickly.
About VBA Editor
- Toolbar: Editor command bar, which is similar to Excel functional area. It contains commands related to Excel VBA development.
- VBA Project: This displays all objects contained in the current VBA project. Usually, a workbook is a VBA project, which includes Excel objects, worksheet objects, modules, and so on.
- Properties window: A window for viewing and setting the properties of the selected object.
- Code editing window: the location where the code is actually written. Writing, modifying, and saving code are all done here.
- Immediate window: During the code running, the printed content will be displayed in the immediate window. Generally Used for Debugging Code.
Manage VBA Projects
Usually, a workbook is a VBA project, which includes Excel objects, worksheet objects, modules, and so on. When multiple workbooks are opened at the same time, they share the same VBA editor, and the VBA project interface displays all VBA projects.
1. Insert/Remove Modules
When you want to insert a new module in a VBA project, you can right-click the VBA project and select the insert type.
If you want to delete the module, you can also right-click and select remove.
2. Modify object/module properties
When clicking any Excel object or module in the VBA project interface, the properties information of the selected object will be displayed in the properties window below. The left side is the attribute name, and the right side is the attribute value. Here you can directly modify the attribute value manually.
The following is the properties window of an Excel worksheet object.
Run the VBA Code
Running VBA code in the editor is probably the most frequently performed operation. In the development process, every time you update the code, you may need to run it to see if the result is correct.
Now there is the following piece of code in module 1 (how to write VBA code will be introduced in the next article):
Sub MyCode()
Sheet1.Range(“A1”) = “Hi Excel”
End Sub
This piece of code means to write the content of “Hi Excel” in cell A1 of the Sheet1.
There are three ways to run VBA code in the editor:
1. Use toolbar commands
Place the cursor at any place of the code to be run, and then select the “Run” → “Run Sub /UserForm” command in the toolbar.
2. Shortcut Toolbar
Place the cursor at any place of the code to be run, and then click the “▶” button in the shortcut toolbar.
3. Use Shortcut Key F5
First, place the cursor at any place of the code to be run, and then use the shortcut key F5 to run the code.
The above content is the basic usage of the VBA editor. I hope you can open an Excel workbook to do it yourself according to the tutorial. This must be more effective than reading the tutorial a hundred times.