Creating a defined name's Refersto range

  • Thread starter Thread starter kittronald
  • Start date Start date
K

kittronald

I'm trying to create a defined name's Refersto range of
"Sheet1!$A$1:$A$5" using the variable below.

r = 5

Workbooks("Book1.xlsb").Names.Add Name:="Test",
RefersTo:=Sheets("Sheet1").Range("A1").Resize(r - 1, 0)

This doesn't work and gives a Run-time error '9': Subscript out of range
error.

What is the code to get the Refersto range properly setup ?


kittronald
 
To start with, you cannot have 0 columns or rows when using resize.

Change the 0 to 1

Workbooks("Book1.xlsb").Names.Add Name:="Test", _
RefersTo:=Sheets("Sheet1").Range("A1").Resize(r - 1, 1)

Second...........your Resize(r - 1, 1) will return A1:A4 when r = 5

Third.........subscript out of range error usually means no such
workbook or worksheet.


Gord
 
I'm trying to create a defined name's Refersto range of
"Sheet1!$A$1:$A$5" using the variable below.

r = 5

Workbooks("Book1.xlsb").Names.Add Name:="Test",
RefersTo:=Sheets("Sheet1").Range("A1").Resize(r - 1, 0)

This doesn't work and gives a Run-time error '9': Subscript out of range
error.

What is the code to get the Refersto range properly setup ?


kittronald

Another way, assuming same file

r=5
sheets("sheet1").range("a1").resize(r-1).name="Test"
 
Back
Top