Microsoft Office Tutorials and References
In Depth Information
Variables, Data Types, and Constants
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
builtin constant ( xlLandscape ) to set the page orientation to landscape for the active sheet:
ActiveSheet.PageSetup.Orientation = xlLandscape
I discovered the xlLandscape constant by recording a macro. I also could have found this
information in the Help system. And, if you have the AutoList Members option turned on, you
can often get some assistance while you enter your code (see Figure 8-2). In many cases, VBA
lists all the constants that you can assign to a property.
Figure 8-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.
The Object Browser, which I discuss briefly in Chapter 7, can display a list of all Excel
and VBA constants. In the VBE, press F2 to bring up the Object Browser.