Here are a few ways to define ranges that change with your data.
All of this stuff has appeared in the newsgroups many times. I thought it
might be
nice to summarise in one place.
1) Just highlight a whole column range. eg range
"A:J" will select all data in columns A through J. Name it Test_r.
You can use this in VBA like this,
Range("Test_r").Select
Or set a range object, and then use it. Like this,
Set tst_rng = Range("Test_r")
tst_rng.Select
2) Use the UsedRange property of the sheet.
ActiveWorkbook.Names.Add
Name:="Test_r", RefersToR1C1:=Sheet1.UsedRange
3) Use End(xlDown) or End(xlToRight) to maintain the range with a little
VBA.
ActiveWorkbook.Names.Add Name:="Test_r", RefersToR1C1:= _
Sheet1.Range(Range("A1"), Range("C1").End(xlDown))
A more complex example of this is shown in the 'Chart Selector' samples
at
http://www.edferrero.com/ExcelCharts...2/Default.aspx
4) Use a dynamic range. eg Suppose your data is in the range "A1:G19", then
define
a new range name called AcData with the following formula;
=Sheet1!$A$1:INDEX(Sheet1!$G:$G, COUNTA(Sheet1!$A:$A))
This assumes that column headings are in row 1, and that column A
contains a value for
every row in the data range ie no null values or blanks.
The dynamic range works because COUNTA(Sheet1!$A:$A) gives the total
number of
rows, and INDEX(Reference,RowNo) points to the cell in 'Reference' given
by 'RowNo'. In our case 'Reference' is all of column G, so if there are
values in A1 to A19, the INDEX function would point to cell G19.
To add a named range, use the menu items Insert-Name-Define... enter the
name for the range and the formula, then click Ok.
With this method you do not need VBA to maintain the range.
5) Similar to (4) you can use the OFFSET function to define a dynamic range.
=OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),6)
This is nice because you can easily make the range dynamic in both
dimensions.
=OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),COUNTA(Sheet1!$1:$1))
Ed Ferrero
www.edferrero.com