Modules

A module is a file that can contain variables, statements, functions, etc. Modules include Python built-in modules, third-party modules, custom modules, class modules, and form modules, among others.

Built-in Modules and Third-Party Modules

【Excel VBA】

Although Excel VBA does not have built-in modules, it can reference third-party modules to extend functionality. Readers can refer to Section 10.2 for details.

【Python】

Chapter 10 introduced the math, cmath, and random modules when discussing functions of built-in modules. These are all Python built-in modules and exist when Python is installed, so no additional installation is required.

In addition to built-in modules, Python has many third-party modules, such as NumPy, Pandas, and Matplotlib. Using third-party modules can significantly improve work efficiency. Third-party modules need to be installed first.

Functional Custom Modules

Functions defined in a functional custom module can be used in other modules.

【Excel VBA】

In the Excel VBA programming environment, select Insert → Module to insert a new module. Enter code in this module, including declarations of module-level or global variables, and procedures or functions.

As shown in Figure 11-1, after adding Module 1, enter code in the right-hand code window to add a function for summing given parameters and a test procedure Test. The Test procedure calls the sum function to calculate the sum of two numbers and outputs the result in the Immediate Window.

Figure 11-1 Module in Excel VBA

When defining the sum function Sum, the Private keyword is used, indicating it is private and can only be called within this module. If the Public keyword is used, it is global and can be called in other modules.

【Python】

In addition to built-in modules and existing third-party modules, Python allows creating modules, i.e., custom modules. The example files provided in this book as files are all custom module files. Custom modules are entered and edited in the Python IDLE script file window.

In the Python command-line window, select File → New File to open the window for writing script files, as shown in Figure 11-2. Enter variables, statements, functions, and classes in this window to complete tasks.

Figure 11-2 Window for Writing Script Files

As shown in Figure 11-2, the window defines a TP function to merge a given list with a new list. Below the function, a list is defined and printed, the TP function is called to merge the list, and the merged result is printed.

Script-Style Custom Modules

Script-style custom modules do not contain functions but use multiple statements to define a continuous sequence of actions.

【Excel VBA】

Excel VBA cannot create script-style custom modules.

【Python】

The example files used in Chapter 5 when introducing flow control are all script-style custom module files.

Class Modules

Both Excel VBA and Python can create class modules. Class modules use code to describe objects in the real world. With classes, instances (objects) can be generated, which are abstractions, simulations, and simplifications of real-world objects based on class code. Due to space limitations, this topic is not elaborated here.

Form Modules

Form modules are used to create graphical user interfaces (GUIs) for programs, facilitating user interaction for inputting and outputting data.

【Excel VBA】

In the Excel VBA programming environment, form modules can be created. Select Insert → UserForm to add a form named UserForm1. First, click the control button in the toolbox, then move the mouse pointer on the gray interface panel to draw the corresponding control. Click the control to set and modify its properties in the Properties list at the bottom left.

As shown in Figure 11-3, the purpose of this design interface is to calculate the sum of data entered in the first two text boxes and display it in the third text box.

Figure 11-3 Design Interface

Double-click the OK button to enter the code window, as shown in Figure 11-4. This is the form module. An event code framework for double-clicking the button is automatically created. Add code to the framework to implement the calculation function.

Figure 11-4 Form Module

Click the triangular button on the toolbar to run the program. The pop-up dialog box is shown in Figure 11-5. This is the interface just designed. Enter 1 and 5 in the first two text boxes and click OK; 6 is output in the third text box.

Figure 11-5 Running the Program

【Python】

Python Shell does not provide functionality to create form modules. GUIs can be created using modules such as Tkinter.