Another major issue that relates to readability is that of modular programming. In the early days of PC programming (in BASIC), most programs were written as a single code unit, sometimes with many hundreds or even thousands of lines of code. It is not easy to follow such a program, especially six months after it was written. Also, these programs tended to contain the same code segments over and over, which is a waste of time and space.

The following BASIC example will illustrate the point. Line numbers have been added for reference. (Don't worry too much about following each line of code. You can still follow the discussion in any case.)

10 ' Program to reverse the letters in your name 20 ' Do first name

30 INPUT "Enter your first name: ", name$ 40 reverse$ = ""

50 FOR i = LEN(name$) TO 1 STEP -1 60 reverse$ = reverse$ + MID$(name$, i, 1)

70 NEXT i

80 PRINT "First name reversed: " + reverse? 9 0 ' Do middle name

100 INPUT "Enter your middle name: ", name? 110 reverse$ = ""

140 NEXT i

150 PRINT "Middle name reversed: " + reverse? 160 ' Do last name

170 INPUT "Enter your last name: ", name? 180 reverse$ = ""

210 NEXT i

220 PRINT "Last name reversed: " + reverse?

Now, observe that lines 40-70, 110-140, and 180-210 (in bold) are identical. This is a waste of space. A better approach would be to separate the code that does the reversing of a string name into a separate code module and call upon that module thrice, as in the following example:

' Program to reverse your name DECLARE FUNCTION Reverse? (name?) ' Do first name

INPUT "Enter your first name: ", name?

PRINT "First name reversed: " + Reverse?(name?)

' Do middle name

INPUT "Enter your middle name: ", name?

PRINT "Middle name reversed: " + Reverse?(name?)

' Do last name

INPUT "Enter your last name: ", name?

PRINT "Last name reversed: " + Reverse?(name?)

The separate code module to reverse a string is:

' Reverses a string FUNCTION Reverse$ (aname$) Temp$ = ""

Reverse$ = Temp$ END FUNCTION

Of course, the saving in space is not great in this example, but you can imagine what would happen if we replace the reversing procedure by one that requires several hundred lines of code and if we want to perform this procedure a few hundred times in the main program. This modularization could save thousands of lines of code.

There is another very important advantage to modular programming. If we decide to write another program that requires reversing some strings, we can simply add our string-reversing code module to the new program, without having to write any new code. Indeed, professional programmers often compile custom code libraries containing useful code modules that can be slipped into new applications when necessary.

It is hard to overestimate the importance of modular programming. Fortunately, as we will see, VBA makes it easy to create modular programs.

Generally speaking, there are two main groups of code modules: functions and subroutines. The difference between them is that functions return a value whereas subroutines do not. (Of course, we may choose not to use the value returned from a function.) For instance, the Reverse function described in the previous example returns the reversed string. On the other hand, the following code module performs a service but does not return a value—it simply pauses a certain number of seconds (given by sec):

SUB delay (sec)

' Get the current time StartTime = TIMER

' Enter a do-nothing loop for sec seconds DO


Functions and subroutines are extremely common in modern coding. Together, they are referred to as procedures.

+2 0


  • EOIN
    What is a modularity in vba?
    1 year ago

Post a comment