Microsoft Office Tutorials and References
In Depth Information
Using Comments in Your Code
x = 1
InterestRate = 0.0625
LoanPayoffAmount = 243089
DataEntered = False
x = x + 1
MyNum = YourNum * 1.25
BanjoStyle = “Clawhammer”
DateStarted = #3/14/2010#
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 (which is used in a For-Next loop) may 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. Data type refers to how data is stored in memory — as integers, logical values,
strings, and so on.
Although VBA can take care of data typing automatically, it does so at a cost — namely, slower
execution and less efficient use of memory. If you want optimal speed for your functions, you
need to be familiar with data types. Generally, it’s best to use the data type that uses the smallest
number of bytes yet still is able to handle all of the data that will be assigned to it. When VBA
works with data, execution speed is a function of the number of bytes that VBA has at its
disposal. In other words, the fewer bytes used by data, the faster VBA can access and manipulate
the data. Table 24-1 lists VBA’s assortment of built-in data types.
Table 24-1: VBA Data Types
Data Type
Bytes Used
Range of Values
1 byte
0 to 255
2 bytes
2 bytes
–32,768 to 32,767
4 bytes
–2,147,483,648 to 2,147,483,647
4 bytes
–3.402823E38 to –1.401298E–45 (for negative values);
1.401298E–45 to 3.402823E38 (for positive values)
8 bytes
–1.79769313486231E308 to –4.94065645841247E–324
(negative values); 4.94065645841247E–324 to
1.79769313486232E308 (positive values)
8 bytes
–922,337,203,685,477.5808 to 922,337,203,685,477.5807
Search JabSto ::

Custom Search