Dear Friends,

This article is intended for Excel VBA beginners. Here in this article I am going to explain you the use of Use Relative Reference option in Excel VBA while recording your macro.

My focus in this article would be in explaining you these two different methods which you can use while recording –
1. Use Relative Reference Method
2. Absolute Method

If you are a beginner, you can also read these articles before reading this article.
What is Excel Macro ?
How to Record / Run Excel Macro ?

What is Relative Reference Option in Macro Recording

Under Developer tab in your Excel workbook, you can see there is a toggle option available below the record macro button as shown in the below picture.
To know how to enable Developer tab in your Excel Workbook, click here

Relative Reference in Excel recording

Relative Reference in Excel recording

As I mentioned this is a toggle button – if this button is highlighted in different color – that mean it is ON else OFF.

Refer the below picture, here this button looks highlighted, that means, if you record your macro now, It will be recorded with Relative reference.

Relative Reference On

Relative Reference On

When Relative Reference is OFF – it is also called Recording in Absolute Mode else it is called Recording with Relative Reference. From this it is clear that there is not a separate button available for Recording in absolute method.

Now, let’s have a close look at the code which is recorded in Absolute method as shown below

Recording Macro with Use Relative Reference – Off



Sub AbsoluteMethod()
'
' AbsoluteMethod Macro
' This macro is recorded with -
' Use Relative Reference --> OFF --> Absolute Method
'

'
    Range("C3").Select
    ActiveCell.FormulaR1C1 = "Name"
    Range("D3").Select
    ActiveCell.FormulaR1C1 = "Vishwamitra"
    Range("C4").Select
    ActiveCell.FormulaR1C1 = "Age"
    Range("D4").Select
    ActiveCell.FormulaR1C1 = "30 Years"
    Range("C5").Select
    ActiveCell.FormulaR1C1 = "Address"
    Range("D5").Select
    ActiveCell.FormulaR1C1 = "Amsterdam"
    Range("C6").Select
    ActiveCell.FormulaR1C1 = "Email Address"
    Range("D6").Select
    ActiveCell.FormulaR1C1 = "[email protected]"
    Range("D7").Select
End Sub



In the above recorded macro you can see that ever cell where values are entered, they are all having the exact address. For example – Range(“C3”), Range(“D3”) etc.

Recording Macro with Use Relative Reference – ON

Same macro, now I am going to record using the Use Relative Reference button ON.


Sub RelativeReferenceMethod()
'
' RelativeReferenceMethod Macro
' This Macro is recorded with -
' Use Relative Reference --> ON --> Relative Reference Method
'

'
    ActiveCell.Offset(-4, -1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Name"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Vishwamitra"
    ActiveCell.Offset(1, -1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Age"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "30 Years"
    ActiveCell.Offset(1, -1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Address"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Amsterdam"
    ActiveCell.Offset(1, -1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "Email Address"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "[email protected]"
    ActiveCell.Offset(1, 0).Range("A1").Select
End Sub


From the above recorded code you can see that all the cells or range addresses are recorded as relative and not absolute. When you are recording your macro using this method, you will see every cell address is referred using Offset Function. To know more about Offset function read this article.

Conclusion

From the above two codes, you can see that the recorded code using first method (Absolute method) is using the exact or absolute address of each cell or cell range whereas in the second method – using Relative Reference turned on, recorded code is not using any exact address. Cells are refered relatively using Offset function.

cover3d_0-89071700_1484285537__1_

Join over 10, 000+ Excel VBA Enthusiasts & get this FREE e-Book Now!