Excel 2019 Power Programming with VBA. Michael Alexander
Читать онлайн книгу.not the same as an
End Sub
statement.
You declare static variables by using the Static
keyword.
Sub MySub() Static Counter as Long '- [Code goes here] - End Sub
Working with constants
A variable's value may change while a procedure is executing (that's why it's called a variable). Sometimes, you need to refer to a named value or string that never changes: a constant.
Using constants throughout your code in place of hard-coded values or strings is an excellent programming practice. For example, if your procedure needs to refer to a specific value (such as an interest rate) several times, it's better to declare the value as a constant and use the constant's name rather than its value in your expressions. Not only does this technique make your code more readable, it also makes it easier to change should the need arise—you have to change only one instruction rather than several.
Declaring constants
You declare constants with the Const
statement. Here are some examples:
Const NumQuarters as Integer = 4 Const Rate = .0725, Period = 12 Const ModName as String = "Budget Macros" Public Const AppName as String = "Budget Application"
The second example doesn't declare a data type. Consequently, VBA determines the data type from the value. The Rate
variable is a Double
, and the Period
variable is an Integer
. Because a constant never changes its value, you normally want to declare your constants as a specific data type.
Like variables, constants have a scope. If you want a constant to be available within a single procedure only, declare it after the Sub
or Function
statement to make it a local constant. To make a constant available to all procedures in a module, declare it before the first procedure in the module. To make a constant available to all modules in the workbook, use the Public
keyword and declare the constant before the first procedure in a module. Here's an example:
Public Const InterestRate As Double = 0.0725
NOTE
If your VBA code attempts to change the value of a constant, you get an error (Assignment to constant not permitted
). This message is what you would expect. A constant is a constant, not a variable.
Using predefined constants
Excel and VBA make available many predefined constants, which you can use without declaring. In fact, you don't even need to know the value of these constants to use them. The macro recorder generally uses constants rather than actual values. The following procedure uses a built-in constant (xlLandscape
) to set the page orientation to landscape for the active sheet:
Sub SetToLandscape() ActiveSheet.PageSetup.Orientation = xlLandscape End Sub
It's often useful to record a macro just to discover the various constants that can be used. And, if you have the AutoList Members
option turned on, you can often get some assistance while you enter your code (see Figure 3.2). In many cases, VBA lists all the constants that you can assign to a property.
FIGURE 3.2 VBA displays a list of constants that you can assign to a property.
The actual value for xlLandscape
is 2
(which you can discover by using the Immediate window). The other built-in constant for changing paper orientation is xlPortrait
, which has a value of 1
. Obviously, if you use the built-in constants, you don't really need to know their values.
NOTE
The Object Browser can display a list of all Excel and VBA constants. In the VBE, press F2 to bring up the Object Browser.
Working with strings
Like Excel, VBA can manipulate both numbers and text (strings). There are two types of strings in VBA.
Fixed-length strings are declared with a specified number of characters. The maximum length is 65,535 characters.
Variable-length strings theoretically can hold up to 2 billion characters.
Each character in a string requires 1 byte of storage, plus a small amount of storage for the header of each string. When you declare a variable with a Dim
statement as data type String
, you can specify the length if you know it (that is, a fixed-length string), or you can let VBA handle it dynamically (a variable-length string).
In the following example, the MyString
variable is declared to be a string with a maximum length of 50 characters. YourString
is also declared as a string; but it's a variable-length string, so its length is not fixed.
Dim MyString As String * 50 Dim YourString As String
Working with dates
You can use a string variable to store a date, but if you do, it's not a real date (meaning that you can't perform date calculations with it). Using the Date
data type is a better way to work with dates.
A variable defined as a date uses 8 bytes of storage and can hold dates ranging from January 1, 0100, to December 31, 9999. That's a span of nearly 10,000 years—more than enough for even the most aggressive financial forecast! The Date
data type is also useful for storing time-related data. In VBA, you specify dates and times by enclosing them between two hash marks (#
).
NOTE
The range of dates that VBA can handle is much larger than Excel's own date range, which begins with January 1, 1900, and extends through December 31, 9999. Therefore, be careful that you don't attempt to use a date in a worksheet that is outside Excel's acceptable date range.
In Chapter 5, “Creating Function Procedures,” we describe some relatively simple VBA functions that enable you to create formulas that work with pre-1900 dates in a worksheet.
About Excel's date bug
It is commonly known that Excel has a date bug: it incorrectly assumes that the year 1900 is a leap year. Even though there was no February 29, 1900, Excel accepts the following formula and displays the result as the 29th day of February 1900:
=Date(1900,2,29)
VBA doesn't have this date bug. The VBA equivalent of Excel's DATE
function is DateSerial
. The following expression (correctly) returns March 1, 1900:
DateSerial(1900,2,29)
Therefore, Excel's date serial number system doesn't correspond exactly to the VBA date serial number system. These two systems return different values for dates between January 1, 1900, and February 28, 1900.
Here are some examples of declaring variables and constants as Date
data types:
Dim Today As Date Dim StartTime As Date Const FirstDay As Date = #1/1/2019# Const Noon = #12:00:00#