Commenting Code

Problem

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

Solution

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.

Категории