Commenting Code


You want to follow good coding practice by documenting your code with comments .


There are two ways to comment code in VBA: use the Rem keyword or the apostrophe (') character to denote a comment, as illustrated in Example 2-9.

Example 2-9. Comments

Public Sub DoRungeKutta( )
 ' Local variables

 Dim y(1 To 4) As Double
 Dim k1(1 To 4) As Double
 Dim k2(1 To 4) As Double
 Dim k3(1 To 4) As Double
 Dim k4(1 To 4) As Double
 Dim a As Double
 Dim n As Integer
 Dim dt As Double
 Dim time As Double

Rem Initialize Variables

 y(1) = 29.8613
 y(2) = 0
 y(3) = 30.0616
 y(4) = 0
 n = 5000
 dt = 5 / n: Rem calculate time step
 time = 0 ' set starting time to zero

End Sub

Example 2-9 shows a portion of a custom subroutine to perform numerical integration using the Runge-Kutta method. This code contains four comments, using both commenting approaches.

You may place comments on the same line as code statements; however, if you use Rem for comments when doing so, you must precede the Rem keyword with a colon (:). You need not use the colon when using the apostrophe comment indicator.

Using Excel

Getting Acquainted with Visual Basic for Applications

Collecting and Cleaning Up Data


Statistical Analysis

Time Series Analysis

Mathematical Functions

Curve Fitting and Regression

Solving Equations

Numerical Integration and Differentiation

Solving Ordinary Differential Equations

Solving Partial Differential Equations

Performing Optimization Analyses in Excel

Introduction to Financial Calculations


Excel Scientific and Engineering Cookbook
Excel Scientific and Engineering Cookbook (Cookbooks (OReilly))
ISBN: 0596008791
EAN: 2147483647
Year: N/A
Pages: 206
Authors: David M Bourg © 2008-2020.
If you may any questions please contact us: