Finance

Charts

Statistics

Macros

Search

Organization of a Program in Excel VBA

A program in VBA consists of one or more modules. Usually, the text of a program in a module begins with directives that control the description of variables, the method of string comparison, and so on. Then come the declarations of module-level or project-level variables and constants, i.e., variables and constants that can be used in all procedures of either the module or the project. After that, the code of Sub and Function procedures, which make up the actual program, is placed.

Below is an example of module organization

Example of a module organization

Option Base 1
Option Explicit

Private Const Pi = 3.14159265358979
Private Out(2) As Double

Private Function CircleLength(r As Double) As Double
    CircleLength = 2 * Pi * r
End Function

Private Function AreaDisc(r As Double) As Double
    AreaDisc = 4 * Pi * r ^ 2
End Function

Private Sub JointResult(r As Double)
    Out(1) = CircleLength(r)
    Out(2) = AreaDisc(r)
    MsgBox Out(1) & vbCr & Out(2)
End Sub

Private Sub ShowResults()
    JointResult 1
End Sub

NOTE
To test the program shown in Listing 2.29, do not forget to first set the Require Variable Declaration parameter on the Editor tab of the Options dialog box in the VBA editor (to open the Options dialog box, use the Tools | Options command in the Visual Basic integrated development environment).

0 0 votes
Évaluation de l'article
S’abonner
Notification pour
guest
0 Commentaires
Le plus ancien
Le plus récent Le plus populaire
Online comments
Show all comments
Facebook
Twitter
LinkedIn
WhatsApp
Email
Print
0
We’d love to hear your thoughts — please leave a commentx