Tutorial 7 Understanding Absolute and Relative Recording in a Spreadsheet Macro

Total Page:16

File Type:pdf, Size:1020Kb

Tutorial 7 Understanding Absolute and Relative Recording in a Spreadsheet Macro

Tutorial 7: Using relative referencing in a spreadsheet macro

This tutorial takes you through the creation of an Excel macro that uses relative references. We’re going to create the MyTitle macro that you created in an earlier tutorial, but this time we’ll use relative references.

It’s a good idea to make sure you can easily do the steps in the macro again, before you actually start recording.

Repeat the recording of the MyTitle macro using relative cell addresses: 1 Select the cell A1. 2 From the Tools menu, select Macro/Record New Macro. 3 Type MyTitle2 in the Macro name box. 4 Type Enters a title into the cell 2 down and 2 right from the selected cell and formats the column in the Description box. 5 Press OK

The macro is now recording, and you should see the Stop Macro button on the screen on its own toolbar. 6 Press the Relative Reference button on the Stop Recording toolbar. 7 Turn off gridlines as follows: From the Tools menu select Options then select the View Tab. From the Windows options clear the Gridlines box and press OK. 8 Enter your name and format it as follows:  Select cell C3 (Note this is 2 down and 2 right from A1) enter YOUR FULL NAME, then press the Enter key.  Reselect the cell C3 and from the Format menu select Cells then select the Font tab.  Select a different Font and change the style to Bold, size to 18.  Press OK 9 Customize the column width as follows: From the Format menu, select Column and AutoFit Selection. 10 Click the Stop Recording button or from the Tools menu and select Macro/Stop Recording. 11 Test out the macro as before, but select a different starting Cell each time.

1 2005

Feedback

You should find that your name is printed in many cells. Each cell is located 2 down and 2 to the right of the cell that was selected when the macro was activated.

The advantage of using the relative cell reference is that you can operate on different groups of cells by selecting a different starting point. This makes you macro much more flexible.

Compare the relative code, shown below, with the previous version of the MyTitle macro that you created in an earlier tutorial.

MyTitle2 macro using relative cell references

Note that the only differences are in the early part with ActiveCell.Offset(2,2) etc. The (2,2) is our 2 down and 2 right location.

It is important to think about the finishing location of the macro. Macros are often used in combinations with other editing and bringing the selection back to the starting point or a point relative to the starting point is also important. The second last line in the code above returns the selection to the original cell 2 up and 2 left (-2, -2).

2 2005

Recommended publications