Hi Kevryl,
'------------------
It could be any range, any range name, as long as when the macro is run, it
creates the rangename relative to where the cursor is then, rather than to
where it was when it was recorded.
But OK, here is a typical simple example, similar to a function I have used
in the past for continuously creating pay advices:
1) start recording the macro,
2) Go to the range "Anchor" (it matters not what the reference is because
it will be different each time - see below step 10)
3) Select "relative" and select the range that includes 8 columns to the
right and 6 rows down ([end][right and [end]down] are a tidy way of
selecting
these)
5) copy the contents to the clipboard
6) move cursor 8 rows down and paste
7) return to "Anchor".
8) Erase the rangename "Anchor"
9) Move cursor 8 rows down
10) Create range name "Anchor"
11) Stop recording
Such a macro works perfectly in the old Lotus and Enable (a Lotus lookalike)
because they record the keystrokes, rather than converting the results of
the
keystrokes to VB before recording. In Excel, the reverse is true and the
"Relative" selection on the macro tool bar is ignored.
To try and clarify the problem further: The first time the macro records the
rangename "Anchor", if I'm on cell A1 then every time I run the macro it
will
return the cursor to cell A1 to recreate the name "Anchor", rather than
moving the cursor 8 rows down to extend the form.
There were so many of these commands that were very easy to use back in the
80's software mentioned. Another nice one was automated cursor movement with
a "wait for input" option (so you could string together a whole automated
sequence of data input anywhere on the worksheet). All these were on a
convenient reference card that came with the software. These days you have
to
learn Visual Basic to do them.
Anyway, I digress

. The question is whether you can give me some simple
macro code for creating a rangename relative to where the cursor is when the
macro is run, rather than to where it was when it was recorded.
'------------------
It is rarely necessary, or desirable, to make selections and is usually
inefficient to do so, If no selections are made, then there is no need to
return the cursor to an initial position.
Try something like:
'============>>
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim destRng As Range
Set WB = Workbooks("MyBook.xls") '<<==== CHANGE
Set SH = WB.Sheets("Sheet1") '<<==== CHANGE
Set Rng = SH.Range("Anchor")
Set destRng = Rng.Offset(8)
Rng.Resize(7, 9).Copy Destination:=destRng
destRng.Name = "Anchor"
End Sub
'<<============
I have assumed that the data range to be copied comprises 7 rows x 9
columns. If, in fact, this should be 6 rows x 8 columns, replace:
Rng.Resize(7, 9).Copy Destination:=destRng
with
Rng.Resize(6, 8).Copy Destination:=destRng