Macro Recording – Absolute References
Open a new workbook and follow these steps to record a simple macro in absolute mode. Before start recording macro, make sure that the Use Relative References
button is not highlighted. This macro simply enters the Monday, Tuesday and Wednesday in a worksheet.
- Click Record Macro button, type
Absolute
as the name for this macro and click OK to begin recording. - Activate cell
A1
, and type Monday. - Move to cell
A2
, and type Tuesday. - Move to cell
A3
, and type Wednesday. - Click cell
A1
to activate it again and stop the macro recording.
Press Alt+F11
to activate the VBE to examine the Module1
module:
Excel generates the following code:
Sub AbsoluteMacro() ' ' Absolute Macro ' Range("A1").Select ActiveCell.FormulaR1C1 = "Monday" Range("A2").Select ActiveCell.FormulaR1C1 = "Tuesday" Range("A3").Select ActiveCell.FormulaR1C1 = "Wednesday" Range("A1").Select End Sub
When executed, this macro selects cell A1
and inserts the Monday, Tuesday and Wednesday into the range A1:A3
. Then the macro reactivates cell A1
.
These same actions occur regardless of which cell is active when you execute the macro. A macro recorded by using absolute references always produces the same results on same range when it’s executed. In this case, the macro always enters the names of the first three days in the range A1:A3
on the active worksheet.
Record Macro – Use Relative References
Sometimes we want our recorded macro to work with cell locations in a relative references mode. For example, we want the macro to start entering the day names in the current active cell. In such a case, we need to use relative recording. To see how relative mode recording works, delete the contents of range A1:A3
and then perform the following steps:
Click the Use Relative References
button to change the recording mode to relative from the Developer tab. Make sure that the Use Relative References
button is highlighted:
- Activate cell
C1
.
As we don’t need to record 1st cell selection so we must activate the beginning cell (C1
) before start the macro recording. - Click Record Macro button, type
Relative
as the name for this macro and click OK to begin recording. - Type Monday (in cell
C1
). - Move to cell
C2
, and type Tuesday. - Move to cell
C3
, and type Wednesday. - Click cell
C1
to activate it again and stop the macro recording.
With the recording mode set to relative, the code that Excel generates is quite different from the code generated in absolute mode:
Sub Relative() ' ' Relative Macro ' ' ActiveCell.FormulaR1C1 = "Mon" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "Tue" ActiveCell.Offset(1, 0).Range("A1").Select ActiveCell.FormulaR1C1 = "Wed" ActiveCell.Offset(-2, 0).Range("A1").Select End Sub
This macro always starts entering text in the active cell. To test, activate any cell and then execute the Relative macro. The day names are always entered beginning at the active cell.
Notice that the code refers to cell A1
even we never used cell A1
during the macro recording. Don’t worry it is the byproduct behavior of macro recording.