SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 31 | Next

Jim DeMarco

"Pro Excel 2007 VBA"

Select
Selection.Font.Bold = True
Range("A6").Select
Selection.Font.Bold = True
End Sub
We select the range containing our data. Each Selection object??™s Font property has a Bold
property that is set to True.
Adding Totals
1. Select the RecordMacro command.
2. Name the macro AddTotals and click OK.
3. Select cell C6, choose the AutoSum command, and then press Enter (AutoSum can be
found on the Home ribbon or the Formulas ribbon, as shown in Figure 1-20).
Figure 1-20. The AutoSum button on the Home ribbon
4. Select cell E2 and choose the AutoSum command. Press Enter.
5. Copy the contents of cell E2 to cells E3:E4. Press Enter.
6. Select cell E6 and choose the AutoSum command. Press Enter.
7. Choose the Stop Recording command from the Developer ribbon.
CHAPTER 1 n THE MACRO RECORDER AND CODE MODULES 17
Taking a look at the code, notice that Excel uses the FormulaR1C1 property of the
ActiveCell object, and this time it makes sense because we are entering formulas. One thing
to note is that, depending on how you copy the formula from cell E2 to the rest of the column
in step 5, Excel will create different lines of code.
Same Task, Different Code
If you use the fill handle and Ctrl-drag the contents into the range E3:E4, the code Excel generates
might look like this:
Range("C6").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
Range("E2").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-2]:RC[-1])"
Range("E2").Select
'Used fill handle to copy formula to E3:E4
Selection.


Pages:
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43