Cover | Table of Contents | Colophon
$ symbol forces Excel to interpret the data as currency. Using E (or e) while entering a number in scientific notation
forces Excel to interpret the string as a number in scientific notation. Entering numbers with dashes between them will cause Excel to interpret the number as a date. I encourage you to try entering various types of data like those I describe here to see how Excel handles the data. In some cases you'll notice that Excel will reformat your data a little. For example, if you type 1.2345e3 in a cell, it will appear as 1.23E+3 in the cell and 1234.56 in the formula bar (when the cell is selected).
=A1+A2.=A1+A2 example. You can enter a formula in the same way you enter text, as discussed in Recipe 1.2. You can either enter the formula directly in a cell (pressing Enter when done), or you can use the formula bar, as discussed earlier. The cell containing the formula will display the result of the formula and not the formula itself. To see the formula, just look at the formula bar when the cell is selected. Or press the F2 shortcut key
to edit the formula directly in the cell, as shown in Figure 1-10.
+, -, /, and *, as well as any number of other built-in functions, as discussed in Recipe 1.10.
=B7*C7; now it's =RC[-2]*RC[-1], which is a relative reference in R1C1 style.R3C5 (i.e., R followed by a number and C followed by another number), you are using absolute cell references. The equivalent in A1 style would be $E$3. Using brackets around the number following either R or C indicates relative cell references. For example, R[1]C[2] refers to the cell one row down and two rows to the right of the cell containing that reference. The cell two rows up and one row to the left would be referred to as A1:A10 refers to the range of cells in column A from row 1 to row 10. The colon character (:) is used to indicate a range reference. The reference A1:B10 refers to the range of cells from column A row 1 to column B row 10. Technically speaking, the cell reference A1 is itself a range of only a single cell; thus, in a sense, all cell references can be thought of as ranges.=A1+B2/C3 in a cell, Excel will perform the division first and then the addition. This may be what you want—that is, you want to add the result of B2 divided by C3 to A1. However, if you intend to divide the sum of A1 and B2 by C3, then you need to write =(A1+B2)/C3. The parentheses force Excel to perform the addition operation first, followed by the division.=((A1+B2)/C3)*C4, and so on. I find it is always good practice to use parentheses liberally to be sure your formula is executed as intended.^) operator.^) is used for exponentiation. For example, to raise the number contained in cell A1 to the third power, you could enter the formula =A1^3. You can use whole number exponents
as well as decimal numbers or even other operations. For example, the formulas =A1^0.25 and =A1^(1/4) both raise the value in cell A1 to the one-fourth power.=A1^C5, or =A1^(C5+D10), or =(A1+A2)^(C5/E8), and so on.+, -, /, *, and ^) are not enough to perform all the calculations you need.ABS( ), SQRT( ), and SIN( ), as needed.NETWORKDAYS function. There are many others, including functions to get the current date and time.
Sheet1, followed by the exclamation mark (!), which precedes the absolute A1-style cell reference. This format, using the sheet name followed by !, makes the reference refer to the specified cell on that specific sheet. Thus, you can use the name in a formula on any other sheet and it will still point to the cell on Sheet1.
Name property in the properties panel (more on that panel in a moment). Each project consists of multiple objects and code modules. Every sheet in a workbook has an associated object shown in the project window, as does the workbook itself. I'll come back to these main, using it as a starting point for subsequent code and calls to other procedures.
Public Sub MySubroutineName(Param1 As Integer, Param2 As Double)
' Your code goes here...
End Sub
Public or Private, followed by the Sub keyword, followed by the subroutine name, which is then followed by an optional parameter list. After the declaration comes the body of the subroutine, which is a collection of code statements that you supply. A subroutine definition is closed with End Sub|
Data type
|
Storage
|
Values
|
|---|---|---|
Boolean
|
2 bytes
|
True or False
|
Byte
|
1 byte
|
0 to 255
|
Integer
|
2 bytes
|
−32, 768 to 32,767
|
Double
|
8 bytes
|
−1.79769313486231 E 308 to +1.79769313486231 E 308
|
String
|
Dim statement to declare a variable, as illustrated in Example 2-4.Dim a As Double Dim n As Integer
Dim
VariableName
As
DataType, where Dim and As are VBA keywords, DataType is one of the VBA data types as discussed in Recipe 2.3, and VariableName is a name you supply that conforms to the naming rules discussed in Recipe 2.2.Dim statements to declare local variables within procedures by including the Dim statements in the body of the procedure. Such local variables can be used only by the procedures within which they are declared. To declare global variables that have module-level scope, place the Dim statements at the beginning of the code module before any procedure declarations. Such global variables can be used by any procedure in the module within which the variables are declared.Public statement like this Public
VariableName
As
DataType.Private statement, but this is the same as using the Dim statement.Dim a As Double, Dim b As Double, Dim c As Double Dim d, e, f As Double
Double type variables, a, b, and cConst
Name
As
DataType
=
Value, as illustrated in Example 2-6.
Const MyConstant As Integer = 14
Public MyConstant2 As Double = 1.025
Public keyword, as illustrated in Example 2-6, when declaring constants at the beginning of a code module to make the constant available to all procedures in the project.Pi throughout your calculations rather than write out the numerical value.PI that returns the value of π. You can actually use that function in your VBA code by accessing the function through the WorksheetFunction object, as in WorksheetFunction.Pi. See Recipe 2.13 for more information on Excel Objects.