![excel macro relative range excel macro relative range](https://www.exceldemy.com/wp-content/uploads/2014/02/relative-reference-macro-record-3-1.png)
Worksheets("Sheet1").Cells(counter, 3).Value = counter * 5
![excel macro relative range excel macro relative range](https://i.stack.imgur.com/MiUOJ.png)
The variable counter is used as the row index for the Cells property. If you actually want to reference a range relative to the currently selected cell(s) then I would use the following: Set rng Selection.Offset(3,4) Note this. The following example fills the first 20 cells in the third column with values between 5 and 100, incremented by 5. To loop through a range of cells, use a variable with the Cells property in a loop. In this example, this macro picks up 2 parameters which are one and two cells to the. once we have the Range of the button cell, we can access everything relative to that cell. To do that, on the Developer tab, click Use Relative References, and then click Record Macro. Function underShape () as Range Dim b As Object Dim r As Range Set b ActiveSheet.Shapes (Application.Caller) Set r b.TopLeftCell Set underShape r End Function. On the Developer tab, select Record Macro. Note You can record macros that use the Offset property to specify relative references instead of absolute references. On the Developer tab, select the Use Relative References option. Sub Underline()ĪctiveCell.Offset(1, 3).Font.Underline = xlDouble In the following example, the contents of the cell that is one row down and three columns over from the active cell on the active worksheet are formatted as double-underlined.
![excel macro relative range excel macro relative range](https://www.exceldemy.com/wp-content/uploads/2014/02/relative-reference-macro-record-1.png)
A common way to work with a cell relative to another cell is to use the Offset property.