Excel Macros - Relative References

Relative reference macros record an offset from the active cell. Such macros will be useful if you have to repeat the steps at various places in the worksheet.

Suppose you are required to analyze the data of voters collected from 280 constituencies. For each constituency, the following details are collected −

The data is provided to you in a worksheet as given below.

Relative References

It is not possible to analyze the data in the above format. Therefore, arrange the data in a table as shown below.

Table

If you attempt to arrange the given data in the above format −

The solution is to record a macro so that you can complete the task in not more than a few seconds. The macro needs to use relative references, as you will move down the rows while arranging the data.

Using Relative References

In order to let the macro recorder know that it has to use relative references, do the following −

Relative Reference

Preparing the Data Format

The first step in arranging the above given data is to define the data format in a table with headers.

Create the row of headers as shown below.

Preparing

Recording a Macro

Record the macro as follows −

Macro Recording

The first data set is arranged in the first row of the table. Delete the rows B6 – B11 and click in the cell B5.

First Data Set

You can see that the active cell is B5 and the next data set will be placed here.

Stop recording the macro. Your macro for arranging the data is ready.

Running a Macro

You need to run the macro repeatedly to complete the data arrangement in the table as given below.

The active cell is B5. Run the macro. The second data set will be arranged in the second row of the table and the active cell will be B6.

Macro Running

Run the macro again. The third data set will be arranged in the third row of the table and the active cell will become B7.

Run the Macro

Each time you run the macro, the active cell advances to the next row, facilitating the repetition of recorded steps at the appropriate positions. This is possible because of the relative references in macro.

Run the macro until all the 280 data sets are arranged into 280 rows in the table. This process takes a few seconds and as the steps are automated, the entire exercise is error free.