VBA

Good VBA code should be easy to maintain and readable. All subroutines and functions should have code to handle errors and all variable names should be meaningful.

This is an example of the code we write. It checks the email address from an input form to make sure that it is well-formed.

The comments on the right illustrate the various points, the line numbers are just for reference.

Line Code Comment
01 Public Function ValidEmail(strAddress As String) As Boolean When used in code the IF statement will read "If ValidEmail(EmailString) Then". This makes the code readable.
02    'Needs VB Script Regular Expressions 5.5 ticked in Tools/references A reminder to the developer that a reference to this library needs to be set using the Tools menu.
03     Const cstrTitle As String = "Valid Email?" The title is used in all error messages to enable the user to report where a bug has manifested itself.
04     Dim oRegEx As RegExp We prefer to declare all variables specifically, this allows for better error checking.
05 On Error GoTo ErrHandler All errors are directed to this label in order to centralise error handling in the block.
06     ValidEmail = False It is good practice to initialise a return varaible at the beginning of the code block.
07     Set oRegEx = New RegExp
08     oRegEx.Pattern = "^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$" This is a fairly simple RegEx for an email address, there are better ones.
09     ValidEmail = oRegEx.Test(strAddress) If the regex returns a zero then the return value will be false, anything else and it will be true.
10 NormalExit:
11     Set oRegEx = Nothing Good practice to explicitly destroy the reference to a temporary variable.
12     Exit Function
13 ErrHandler:
14     MsgBox Err.Description & vbCrLf & "Error number: " & Err.Number, vbExclamation, cstrTitle Good practice to explicitly report the error description and the error number.
15     Resume NormalExit
16 End Function

Code must be password protected to safguard the client's intellectual property.

VBA, whilst not having many of the whistles and bells of later languages, is a first rate rapid application development language. You will be up and running using VBA in half the time it takes to write in other languages.