Relative Reference NAME Ranges

Gunswick

Board Regular
Joined
Jun 18, 2008
Messages
181
How can I make creating a named range relative to the active cell? So that I can click on any cell and a named range is created in the adjacent column for 5 rows.
e.g. click cell c5, and a named range from cells d5:d10 is created when i run the macro

Code:
ActiveCell.Offset(0, 1).Range("A1:A11").Select
    ActiveWorkbook.Names.Add Name:="fygainloss", RefersToR1C1:= _
        "='2007'!R5C3:R15C3"

this is what i have so far in my macro, I cant figure how to make it set the range specified by the offset, it always just uses absolute ranges from cell A1.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Code:
    ActiveWorkbook.Names.Add Name:="fygainloss", RefersToR1C1:= _
        "='2007'!RC[1]:R[5]C[1]"
should work.
 
Upvote 0
that works perfectly, thank you very much!

what about making the name unique? so that I could create multiple named ranges.

i dont mind what the names actually are, as long as they are unique. perhaps a random number generator or an incremental counter?

(this is to help make copying charts with different data sources easier by the way).
 
Upvote 0
Ah, I think I may have misunderstood. That creates a named range that is always one column to the right of the activecell and 6 rows high. So if you select D5, it refers to E5:E10. If you want a fixed reference that is established relative to the cell that is active at the time, then you need something like this:
Code:
    ActiveWorkbook.Names.Add Name:="fygainloss" & ActiveCell.Address(0, 0), _
         RefersTo:="='2007'!" & ActiveCell.Offset(0, 1).Resize(6).Address
 
Upvote 0
its basicly so that i can copy a template chart for about 30 different records, which are all on one sheet.

so that i can just click on the top left cell of each "record", i.e the information that will go into the chart (e.g. 2007 gains + losses, 2008 gains+losses etc)

so i need to combine what you wrote for me...

Code:
ActiveWorkbook.Names.Add Name:="fygainloss", RefersToR1C1:= _
        "='2007'!RC[1]:R[10]C[1]"

and..

Code:
Sheets("CES Eur Frt07").Select
    Sheets("CES Eur Frt07").Copy After:=Sheets(13)
    ActiveChart.ChartArea.Select
 
ActiveChart.SeriesCollection(1).Values = _
        "='FFR Forecasting Accuracy 2007 and 08.xls'!fygainloss"

which works, it copies the chart and updates the data (there are actually 8 series, but its the same thing repeated 8 times).

so i can now just click on the top left of each record and it makes a chart (rather than manually editing the data source of each copied chart).

However I need to make 30 individual ones, so obviously if i set new ranges for the same names, i will have 30 charts with the same info! = uselss :LOL:

so i need to make the name unique, so that i can create each chart uniquely.

I dont think randomise will do it well, so i think i need an counter for the name. e.g. fygainloss1, fygainloss2, fygainloss3 etc for each named range i create with your code :cool:
 
Upvote 0
The version I just posted appends the cell address to the name of the range so they will all be unique.
 
Upvote 0
ah yes i see, i didnt understand it before. It uses offset instead of the rc[0]r[1]c[0] etc

so that gives a unique name, which i need. So then how do i get it into my series change code

Code:
Sheets("CES Eur Frt07").Select
    Sheets("CES Eur Frt07").Copy After:=Sheets(13)
    ActiveChart.ChartArea.Select

ActiveChart.SeriesCollection(1).Values = _
        "='FFR Forecasting Accuracy 2007 and 08.xls'!fygainloss"

so that fygainloss is fygainlossActiveCellAddress (i.e. the unique name we just created) ?
 
Upvote 0
Dunno! How do you know which range relates to which chart?
 
Upvote 0
as an example, i name the range and as you intended it becomes fygainlossB47 for example

so i need to get fygainlossB47 into my data series code in the chart, else the charts are not unique. can we use the active cell code you used before? fygainloss" & ActiveCell.Address(0, 0)

e.g.

Rich (BB code):
Sub chartcopy8()
 
'set my range as a unique name (thanks rorya)
      ActiveWorkbook.Names.Add Name:="fygainloss" & ActiveCell.Address(0, 0), _
         RefersTo:="='2007'!" & ActiveCell.Offset(0, 1).Resize(11).Address
 
 
'selects my template chart, and makes a copy
     Sheets("CES Eur Frt07").Select
    Sheets("CES Eur Frt07").Copy After:=Sheets(13)
    ActiveChart.ChartArea.Select
 
'changes the series range from the original to my new unique name range 
'(should be fygainlossB47 for example)
      ActiveChart.SeriesCollection(1).Values = _
        "='FFR Forecasting Accuracy 2007 and 08.xls'!fygainloss"      
 
End Sub
 
Last edited:
Upvote 0
Try this:
Code:
   Dim strName As String
   strName = "fygainloss" & ActiveCell.Address(0, 0)
'set my range as a unique name (thanks rorya)
      ActiveWorkbook.Names.Add Name:=strName, _
         RefersTo:="='2007'!" & ActiveCell.Offset(0, 1).Resize(11).Address
 
 
'selects my template chart, and makes a copy
     Sheets("CES Eur Frt07").Select
    Sheets("CES Eur Frt07").Copy After:=Sheets(13)
    ActiveChart.ChartArea.Select
 
'changes the series range from the original to my new unique name range
'(should be fygainlossB47 for example)
      ActiveChart.SeriesCollection(1).Values = _
        "='FFR Forecasting Accuracy 2007 and 08.xls'!" & strName
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top