Recording Macro – Relative References and Absolute Method

.

Dear Friends,

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

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 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

Relative Reference in Excel recording

Relative Reference in Excel recording

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.

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 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

Macro Recording - Absolute Method -Off

Macro Recording – Absolute Method -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.

Buy a coffee for the author

Adsense

Download FREE Tools and Templates

There are many cool and useful excel tools and templates available to download for free. For most of the tools, you get the entire VBA code base too which you can look into it, play around it, and customize according to your need.

Convert PDF Tables to Excel with VBA | Step-by-Step Guide
Convert PDF Tables to Excel with VBA | Step-by-Step Guide

Convert PDF Tables to Excel with VBA | Step-by-Step Guide How to Convert PDF Tables to Excel Using Excel VBA (Macros) Extracting data from PDF tables into Excel can be tedious, but with Excel VBA (macros), you can automate this process and save time. This guide will...

Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide
Dynamic Arrays and Spill Functions in Excel: A Beginner’s Guide

In today's tutorial, we'll be diving into the exciting world of dynamic arrays and spill functions in Office 365 Excel. These features have revolutionized the way we work with data, providing a more flexible and efficient way to handle arrays. I am going to explain...

How to Declare a Public Variable in VBA
How to Declare a Public Variable in VBA

While programming in VBA sometimes you need to declare a Public Variable that can store the value throughout the program. Use of Public Variable: Let's say you have 4 different Functions in your VBA Code or Module and you have a variable that may or may not be...

What is Excel Formula?

Excel Formula is one of the best feature in Microsoft Excel, which makes Excel a very very rich application. There are so many useful built-in formulas available in Excel, which makes our work easier in Excel. For all the automated work, Excel Macro is not required. There are so many automated things can be done by using simple formulas in Excel. Formulas are simple text (With a Syntax) which is entered in to the Excel Worksheet Cells. So how computer will recognize whether it is a formula or simple text? Answer is simple.. every formula in Excel starts with Equal Sign (=).

You May Also Like…

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join and get a FREE! e-Book

Don't miss any articles, tools, tips and tricks, I publish here

You have Successfully Subscribed!

Pin It on Pinterest