Suppose you’ve recorded a macro that enters a series of labels, sets their font, and then draws
a border around them. Then you discover that you forgot a step or that you recorded a step
incorrectly—you chose the wrong border format, for example. What do you do?
To add code to an existing macro, you can record actions in a temporary macro and then transfer
the code into the macro you want to change.
For example, to add to the MyPolyAddress macro
a step that sets font options for my name, follow these steps:
Sub MyPolyAddress() Range("A2").Select ActiveCell.FormulaR1C1 = "Prof. Ming Leung" With Selection.Font .Name = "Arial" .FontStyle = "Bold Italic" .Size = 14 .Strikethrough = False .Superscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ColorIndex = xlAutomatic End With Range("A3").Select ActiveCell.FormulaR1C1 = "Polytechnic University" Range("A4").Select ActiveCell.FormulaR1C1 = "Six Metrotech Center" Range("A5").Select ActiveCell.FormulaR1C1 = "Brooklyn, NY 11201" Range("A6").Select End SubTo test the macro, return to Excel (press Alt+F11 or select the Excel button on the taskbar). Clear the names and address that you entered earlier when you recorded the macro. Then press Ctrl+Shift+A.
Selection.Font.Name = "Arial" Selection.Font.FontStyle = "Bold Italic" Selection.Font.Size = 14and so on. In the MyPolyAddress macro, the ActiveCell object and the Selection object both refer to the same range on the worksheet, cell A6. Because you can apply a series of font formatting options to an entire range, Excel records the action with Selection, rather than with ActiveCell. Enclosing the property assignments within the With … End With structure simplifies the code and also makes it run faster.