In inputting data in tabular form it frequently happens that the number or text to be input into a cell is exactly the same as what was input into the cell immediately above it. Excel offers a variety of possibilities for copying into the
Excel can distinguish in macro recording between absolute and relative movement between
Absolute recording is the default mode. When you move the cell pointer during recording from B2 to D4, say, the resulting command is Range("D4").Select .
In relative mode, however, the command would be
ActiveCell.Offset(2, 2).Range("A1").Select
: Here
ActiveCell.Offset(2, 2)
denotes the cell two rows
The differences between these two variants become apparent when the macros are executed. In the first case cell D4 will always be involved, irrespective of where the cell pointer is located. In the second case the affected cell is to be
Since Excel 97 there has been no menu command for switching between relative and absolute recording. Instead, one must click on the Relative Reference tool in the "stop recording" toolbar. This toolbar appears automatically when you begin recording a macro. If the tool appears as a pressed-in button, then relative recording is in effect,
Figure 1-6:
The button on the right toggles between absolute and relative recording
Before you begin recording the macro, prepare the table: Input some text into a cell, and then move the cell pointer into the cell immediately below.
Begin recording by executing ToolsMacroRecord New Macro, giving the macro the
While the recorder is running, execute the following keyboard entries and commands: Shift+
‘
(this selects the current cell and the cell lying above it), EditFillDown (this copies to the cell below), and finally,
’
(to move the cell pointer to the next cell to the right, where the
The following Visual Basic macro can now be found in the personal macro workbook:
' shortcut.xls
Sub CopyFromCellAbove()
ActiveCell.Offset(-1, 0).Range("A1:A2").Select
ActiveCell.Activate
Selection.FillDown
ActiveCell.Offset(0, 1).Range("A1").Select
End Sub
If you now try out the macro, you will see that while in principle it runs, the cell pointer ends up one cell too high, to the right of the cell above rather than the cell below. There seems to be a
ActiveCell.Offset(1, 1).Range("A1").Select
| Note |
If during recording you
|