Programming Style

This book uses the following programming style guidelines for code examples.

The following font is used for code.

Sub HelloWorld

Cells(1,1).Value = "Hello, world!"

End Sub

An apostrophe (') introduces comments in code.

' This is a comment; these two lines

' are ignored when the program is running.

Names of macros and user-defined functions appear with initial letters capitalized throughout this book. Note that macro and function names cannot include spaces, so if a name consists of more than one word, the other words in the name also have their initial letters capitalized.

' The AuditResult user-defined function is in the Finance module.

Function AuditResult(latestIncome, expenses, taxes, commissions)

Argument and variable names appear with initial letters lowercase (to distinguish them from macro, function, property, method, and object names).

Keywords appear with initial letters capitalized, whereas built-in constants appear with an initial lowercase "xl" or "vb."

' Sub is a keyword.

Sub Title(titleText)

' xlManual is a built-in constant.

Application.Calculation = xlManual

Control-flow blocks and statements in Sub and Function procedures are indented within the code that surrounds them.

Sub CheckRecordSound

SoundRecordCapable = Application.CanRecordSounds

If SoundRecordCapable Then

Cells(1,1).SoundNote.Record

End If

End Sub

The line-continuation character — an underscore ( _ ) — indicates that code continued from one line to the next is part of the same logical line. You can type these statements all on one line in the Visual Basic module. You can also divide lines of code and add the line-continuation character yourself.

ActiveSheet.Rectangles.Add _

width:=200, _

height:=200, _

left:=50, _

top:=50