Vba Programming Basics

Exl ra

VBA provides over 700 built-in constants, which you can insert into your code at any point without declaring them. The Excel VBA object model adds another 1,266, all of which begin with either xl or vb. You can use these constants anywhere, and you do not need to know their actual values in order to use them. Two of the most commonly used VBA constants deal with inserting carriage returns, vbCrLf, and tab characters, vbTab, in your output. Although each of these constants have a numeric equivalent, you simply type the name of the appropriate constant value in your code. To find a list of all VBA and Excel VBA Object Model constants, press F2 to view the Object Browser and search for Constant. Most of the constant values are self-explanatory, based upon the name. Appendix A also includes many of the constant values that you use throughout this book. Many parameter values require a specific type of constant value. For example, with the MsgBox function you use one of the vbMsgBoxStyle constants for the value of the Buttons parameter to indicate the type of buttons on the message box, as follows:

Example:

MsgBox("Select button", vbYesNoCancel)

] File Edit View Insert Format Debug Run Tools

] File Edit View Insert Format Debug Run Tools

Excel Vba Programming

■ The message box displays the value to the variable you specified in step 6.

0 Type an equal sign (=) and a starting value for your variable.

-O Type MsgBox(ConstName), replacing ConstName with the name of your Const.

Note: See Chapter 7 for more information on the MsgBox function.

• Run the corresponding macro in Excel.

■ The message box displays the value to the variable you specified in step 6.

0 0

Post a comment