Excel

Page 112

Chapter 7: Essential VBA Language Elements Everyone develops his or her own style of commenting. To be useful, however, comments should convey information that’s not immediately obvious from reading the code. Otherwise, comments just chew up bytes and make files larger than necessary. The following tips can help you make effective use of comments: Briefly describe the purpose of each Sub or Function procedure you write. Use comments to keep track of changes you make to a procedure. Use a comment to indicate that you’re using a function or a construct in an unusual or nonstandard manner. Use comments to describe the variables you use, especially if you don’t use meaningful variable names. Use a comment to describe any workarounds you develop to overcome bugs in Excel. Write comments as you develop code, instead of saving the task for a final step.

Using Variables, Constants, and Data Types VBA’s main purpose is to manipulate data. VBA stores the data in your computer’s memory; it may or may not end up on disk. Some data, such as worksheet ranges, resides in objects. Other data is stored in variables that you create.

Understanding variables A variable is simply a named storage location in your computer’s memory. You have lots of flexibility in naming your variables, so make the variable names as descriptive as possible. You assign a value to a variable by using the equal sign operator. (More about this later in the “Using Assignment Statements” section.) The variable names in these examples appear both on the left and on the right side of the equal signs. Note that the last example uses two variables.

95


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.