Creating a defined name's Refersto range

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
 
G

Gord Dibben

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
 
D

Don Guillett

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top