Record a macro that works from any cell
Record and run macros9 minPremium
Builds on Record your first macro.
A stock controller at a garden-supply warehouse in Utrecht ticks pallets off a list as they leave. On each pallet's row she types her initials in column C and Shipped in column D, and she wants a macro that does it on whichever row she's on.
How it works
By default, the recorder writes down the addresses you click. Click C3 while recording, and the macro goes to C3 every time it runs, whatever cell you're in.
Use Relative References, on the Developer tab, changes that. While it's on, the recorder notes each move as a distance from the active cell: two cells to the right, one down. Run the macro with A5 selected, and the same moves start from A5.
Select the starting cell first, turn on Use Relative References, then select Record Macro, do the steps and stop. The button stays on until you select it again, so check it before your next recording.
Example
With the pallet number in A3 selected, she records her initials and the status with Use Relative References on.
Sub MarkShipped() ActiveCell.Offset( 0, 2). Range( "A1").Select ActiveCell.FormulaR1C1 = "MV" ActiveCell.Offset( 0, 1). Range( "A1").Select ActiveCell.FormulaR1C1 = "Shipped" End Sub
ActiveCell.Offset( is 0 rows down and 2 columns right of the active cell. The .Range( the recorder adds after it means that cell itself, not A1 of the sheet. The same steps recorded with the button off would read Range(.
Common mistakes
- Clicking the starting cell after you start recording. The click becomes the macro's first move, measured from wherever you were before.
- Leaving Use Relative References on for the next recording. A macro meant to fill the same cells every time then moves with the selection.
- Running a relative macro from the wrong cell. It starts from the active cell, so select the same kind of cell you started from.
In short
- Turn on Use Relative References before you record, to record moves instead of addresses.
- Select the starting cell before you start recording.
Offset(counts from the active cell: rows first, then columns.rows, columns)
Practice
Premium4 exercises: 1 quiz question, 2 macros and 1 set of steps.
Practice is part of Premium
Every lesson is free to read, and you can practice the free lessons without an account. This lesson's exercises are part of Premium, which you add to a free account.
Have one? Sign in