Commenting Blocks and Loops

Using nested block and loop structures is often a necessary part of any VBA macro. Multiple nested structures can be very long and difficult to follow when trying to read and debug the code. To aid in the legibility of your code always indent the code within the block or loop structure. For longer running structures copy the initial test expression and paste it as a comment after the closing statement. See the example macro below.

‘Example showing use of indents and comments
Public Sub MyMacro()
    Dim FileIsOpen As Boolean
    Dim iCount As Integer

    'Do stuff here

    'Indent lines of code under every block and loop
    'Add comment after block/loop closing statement
    If FileIsOpen Then

        For iCount = 0 To 10
            Select Case iCount
                Case 0
                    'Indent 0 condition code here
                Case 10
                    'Indent 10 condition code here
                Case Else
                    'Indent Else condition code here
            End Select    'iCount

        Next    'iCount = 0 To 10
    Else
        MsgBox "File is closed", vbInformation, "MyMacro"

    End If   'FileIsOpen

    'Do more stuff here
End Sub

EnvisionCAD

Since 1996, EnvisionCAD has been a nationally recognized leader in the configuration, customization, implementation, training & support for CAD software solutions. Our individualized approach has benefited private engineering firms and government agencies alike. Basic or advanced, we can help you get the most from your CAD technology.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *