VBA basis - functions and modules

Overview

For general VBA program, it may be one or several functions on the line. After all, excel VBA only as an auxiliary tool to use.

But with VBA to write more and more, with a project to manage it is necessary, while the basic code of the project is to function and modules .

function

The VBA function has two kinds, one is Sub , one is Function in VBA, in fact, Sub called the process, do not call the function, the difference between them there are two:

  1. Function returns a value, Sub no
  2. Sub can be executed directly, Function must be called to perform

Sub and Function of the examples are as follows:

 1  Option Explicit
 2  
 3  Sub CallAnotherMod()
 4      Dim sum As Integer
 5      sum = Add(2, 3)
 6      Debug.Print sum
 7  End Sub
 8  
 9  Function Add(a As Integer, b As Integer) As Integer
10      Debug.Print a
11      Debug.Print b
12      Add = a + b
13  End Function

Note that the return value of the function, the result is assigned to the function name itself the Add = A + b , otherwise return value is always 0

Module

Modular aim is to reuse for some common functionality, we can be packaged into modules and export. In this way, import existing VBA module in a different project, they do not duplicate-create the wheel.

Export module is very easy, just right-click on the corresponding module can be exported, the export is a * .bas file.

General module

General module simple, we wrote before the Sub or Function in fact, in a default general module. The following example project, I created two modules, namely the Main and MyMath, Main used to test, MyMath in order to reuse later function used.

  • Main modules:

    1  Option Explicit
    2  
    3  Sub CallAnotherMod()
    4      Debug.Print "2 + 3 = " & (Add(2, 3))
    5      Debug.Print "2 * 3 = " & (Multiply(2, 3))
    6  
    7  End Sub
  • MyMath modules:

    1  Option Explicit
    2  
    3  Function Add(A As Integer, B As Integer) As Integer
    4      Add = A + B
    5  End Function
    6  
    7  Function Multiply(A As Integer, B As Integer) As Integer
    8      Multiply = A * B
    9  End Function
  • Main module operation results are as follows:

    2 + 3 = 5
    2 * 3 = 6

Class Module

In addition to the package as above, some functions through the function, can follow the VBA class way to organize, familiar with object-oriented design developers may prefer this embodiment.

  • Create a class module, a new module, and the same general steps, except the previously selected is a new type of class module
  • For example, we create a new class module MathCls:

     1  Option Explicit
     2  
     3  ' 常量, class说明
     4  Const MathClsName = "math class"
     5  
     6  Dim MathA As Integer
     7  Dim MathB As Integer
     8  
     9  ' 相当于其他面向对象语言中的 set 方法     
    10  Public Property Let A(numA As Integer)
    11      MathA = numA
    12  End Property
    13  
    14  ' 相当于其他面向对象语言中的 get 方法     
    15  Public Property Get A() As Integer
    16      A = MathA
    17  End Property
    18  
    19  ' 相当于其他面向对象语言中的 set 方法     
    20  Public Property Let B(numB As Integer)
    21      MathB = numB
    22  End Property
    23  
    24  ' 相当于其他面向对象语言中的 get 方法     
    25  Public Property Get B() As Integer
    26      B = MathB
    27  End Property
    28  
    29  Function Add() As Integer
    30      Add = MathA + MathB
    31  End Function
  • Used to test the Main module, which is a common module

     1  Option Explicit
     2  
     3  Sub CallAnotherMod()
     4  
     5      Dim mc As New MathCls
     6      mc.A = 2
     7      mc.B = 3
     8  
     9      Debug.Print (mc.Add())
    10  
    11  End Sub

to sum up

Regardless of the kind of approach to the package, the package need to look for a function that way. Like functional and object-oriented programming, like these two ways, no one is higher or lower, each with the appropriate scene.

Guess you like

Origin www.cnblogs.com/wang_yb/p/11613069.html