This article is intended for Excel VBA beginners. Here in this article, I am going to explain to you the use of the Use Relative Reference option in Excel VBA while recording your macro.
My focus in this article would be on explaining you these two different methods which you can use while recording –
1. Use Relative Reference Method
2. Absolute Method
What is Excel Macro ?
How to Record / Run Excel Macro ?
What is Relative Reference Option in Macro Recording
Under the 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
As I mentioned this is a toggle button – if this button is highlighted in a different color – that means it is ON else OFF.
Refer to the below picture, here this button looks highlighted, which means, if you record your macro now, It will be recorded with Relative reference.
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 the absolute method.
Now, let’s have a close look at the code which is recorded in the 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 = "info@learnexcelmacro.com"
Range("D7").Select
End Sub
In the above-recorded macro you can see that in every 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 = "info@learnexcelmacro.com"
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 to using Offset Function. To know more about the Offset function read this article.
Conclusion
From the above two codes, you can see that the recorded code using the 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, the recorded code is not using any exact address. Cells are referred to relatively using the Offset function.
0 Comments