It is a good idea to document your code very well so that you and others will be able to understand the purpose of each procedure and why it was written in a certain way. You can document your code in several ways. The ideal way to document your code is to use a general comment section at the beginning of each procedure and to use in-line comments to describe each segment of code. Here is an example of such a general comment section.
'***************************************************************** '* '* Procedure Name: TestProcedure '* '* Purpose: The purpose of this procedure is to illustrate why '* documenting your code is so important. '* '* Parameters: None. '* '* Date Created: September 4, 2006 '* By: Denise M. Gosnell '* '* Modification Log: '* Date Explanation of Modification Modified By '* ------------ ---------------------------------- --------------------- '*'******************************************************************
The preceding documentation style may be overkill if the procedure itself is only a few lines of code. Some type of general comment section should be used with all procedures, even if it is not as detailed as the one in the preceding code.
You have seen in-line comments in all the code examples throughout this chapter. Here is another example:
'display a message to the user Msgbox "Demonstration of In-Line commenting"
It is always better to write too many comments than too few. As you gain more experience writing VBA code, you will see how difficult it can be to follow what is happening in a particular procedure. This is especially true if you go back to the code at a later date and no longer remember the details about why you wrote it a particular way, or if you are reviewing code that someone else wrote. Code comments make this process much easier.