Excel 2019 Power Programming with VBA. Michael Alexander
Чтение книги онлайн.

Читать онлайн книгу Excel 2019 Power Programming with VBA - Michael Alexander страница 37

Название: Excel 2019 Power Programming with VBA

Автор: Michael Alexander

Издательство: John Wiley & Sons Limited

Жанр: Программы

Серия:

isbn: 9781119514916

isbn:

СКАЧАТЬ

       Use comments to describe the purpose of variables so that you and other people can decipher otherwise cryptic names.

       Use comments to describe workarounds that you develop to overcome Excel bugs or limitations.

       Write comments while you code rather than afterward.

       When you've completed all coding, take some time to go back and tidy up your comments, removing any comments that are no longer needed and expanding on comments that may be incomplete or a bit too cryptic.

      TIP

      In some cases, you may want to test a procedure without including a particular instruction or group of instructions. Instead of deleting the instruction, convert it to a comment by inserting an apostrophe at the beginning. VBA then ignores the instruction when the routine is executed. To convert the comment back to an instruction, just delete the apostrophe.

      The Visual Basic Editor offers an Edit toolbar containing tools to assist you in editing your code. In particular, there are two handy buttons that enable you to comment and uncomment entire blocks of code at once.

      Note that the Edit toolbar isn't displayed by default. To display this toolbar, choose View ⇨ Toolbars ⇨ Edit.

      You can select several lines of code at once and then click the Comment Block button on the Edit toolbar to convert the selected lines to comments. The Uncomment Block button converts a group of comments back into uncommented code.

      VBA's main purpose is to manipulate data. Some data resides in objects, such as worksheet ranges. Other data is stored in variables that you create.

      You can think of a variable as a named storage location in your computer's memory. Variables can accommodate a wide variety of data types—from simple Boolean values (True or False) to large, double-precision values (see the following section). You assign a value to a variable by using the equal sign operator (more about this process in the upcoming section “Assignment Statements”).

      You make your life easier if you get into the habit of making your variable names as descriptive as possible. VBA does, however, have a few rules regarding variable names.

       You can use alphabetic characters, numbers, and some punctuation characters, but the first character must be alphabetic.

       VBA doesn't distinguish between case. To make variable names more readable, programmers often use mixed case (for example, InterestRate rather than interestrate).

       You can't use spaces or periods. To make variable names more readable, programmers often use the underscore character (Interest_Rate).

       You can't embed special type declaration characters (#, $, %, &, or !) in a variable name.

       Variable names can be as long as 254 characters—but using such long variable names isn't recommended.

      The following list contains some examples of assignment expressions that use various types of variables. The variable names are to the left of the equal sign. Each statement assigns the value to the right of the equal sign to the variable on the left.

      x = 1 InterestRate = 0.075 LoanPayoffAmount = 243089.87 DataEntered = False x = x + 1 MyNum = YourNum * 1.25 UserName = "Bob Johnson" DateStarted = #12/14/2012#

      VBA has many reserved words, which are words that you can't use for variable or procedure names. If you attempt to use one of these words, you get an error message. For example, although the reserved word Next might make a very descriptive variable name, the following instruction generates a syntax error:

      Next = 132

      Defining data types

      VBA makes life easy for programmers because it can automatically handle all the details involved in dealing with data. Some programming languages, however, are strictly typed, which means that the programmer must explicitly define the data type for every variable used.

      Data type refers to how data is stored in memory—as integers, real numbers, strings, and so on. Although VBA can take care of data typing automatically, it does so at a cost: slower execution and less efficient use of memory. As a result, letting VBA handle data typing may present problems when you're running large or complex applications. Another advantage of explicitly declaring your variables as a particular data type is that VBA can perform some additional error checking at the compile stage. These errors might otherwise be difficult to locate.

Data Type Bytes Used Range of Values
Byte 1 byte 0 to 255.
Boolean 2 bytes True or False.
Integer 2 bytes –32,768 to 32,767.
Long 4 bytes –2,147,483,648 to 2,147,483,647.
Single 4 bytes –3.402823E38 to –1.401298E-45 (for negative values); 1.401298E-45 to 3.402823E38 (for positive values).
Double 8 bytes –1.79769313486232E308 to –4.94065645841247E-324 (negative values); 4.94065645841247E-324 to 1.79769313486232E308 (for positive values).
Currency 8 bytes –922,337,203,685,477.5808 to СКАЧАТЬ