官术网_书友最值得收藏!

Creating a module for storing a Sub procedure

In this recipe, we will be creating a new module, which is yet another object, in the Project window. So far, we've seen only two objects in the Project window: Sheet1 and ThisWorkbook. It is possible to create a code window for each, but Sub procedures must be created and stored in a module in the Project window. This is where coding for all objects can be created and saved.

Getting ready

Make sure that Excel is open and that the VBA Editor is visible.

How to do it…

The steps for this recipe are as follows:

  1. With Excel open on a new sheet, select the Developer tab. In the Code group, select the Visual Basic icon to activate the VBA Editor. If you prefer using the shortcut key, simply press Alt + F11.

    In the VBA Editor, observe the Project window. Under Microsoft Excel Objects, only two objects are visible: Sheet1 (Sheet1) and ThisWorkbook.

  2. On the menu bar, select Insert. From the drop-down menu, select Module, as shown:

Figure 2.14 – The Insert menu's Module option

Once you've made your selection, the VBA Editor adds a new module – visible in the Project window – and a new code window appears on the right:

Figure 2.15 – Newly inserted module

How it works…

When you open a new spreadsheet in Excel and then activate the VBA Editor, no modules will be visible in the Project window.

However, should you record a macro and then activate the VBA Editor, that macro will be stored in a module.

Finally, if you choose not to record a macro, but to insert the code yourself, you have to create a module yourself.

主站蜘蛛池模板: 库尔勒市| 合阳县| 连江县| 徐闻县| 会理县| 湘潭市| 尼玛县| 西和县| 嵊州市| 庐江县| 五原县| 元谋县| 汪清县| 阳谷县| 凤阳县| 孟连| 大同县| 农安县| 曲阳县| 和政县| 大埔县| 彭泽县| 吐鲁番市| 琼结县| 固阳县| 云龙县| 报价| 保定市| 泸定县| 东阿县| 盐池县| 罗山县| 枣强县| 东乡族自治县| 昭苏县| 双鸭山市| 都兰县| 阿荣旗| 凤城市| 广宁县| 太康县|