VBA Code to name a variable range

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a macro to create charts. Problem is that the source data is variable
by lenght....could be between 10-15 rows. Columns are always constant.

I can't use the code
ActiveWorkbook.Names.Add Name:="RangeName", RefersTo:=Selection
because you have to specify the cell refs.

Any ideas please>?
 
hi,
assumeing that your source data in a A1 down......
the key would be to select the variable range first. try something like
this....
Sub macSetRName()
Sheets("YourSheet").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="TheRangeName", RefersTo:=Selection
End Sub
Post back if this don't work for you.

FSt1
 
You could use similar to this...

ActiveWorkbook.Names.Add Name:="RangeName", _
RefersTo:=Sheet1.Range("A1", Sheet1.Range("D65536").End(xlUp))
 
Alternative

Worksheets("Sheet1").Range("D1").Resize(Cells(Rows.Count,"D").End(xlUp).Row)
.. _
Name = "RangeName"


--

HTH

RP
(remove nothere from the email address if mailing direct)
 
This is spot on
Cheers
John

FSt1 said:
hi,
assumeing that your source data in a A1 down......
the key would be to select the variable range first. try something like
this....
Sub macSetRName()
Sheets("YourSheet").Select
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="TheRangeName", RefersTo:=Selection
End Sub
Post back if this don't work for you.

FSt1
 
or withOUT selections from anywhere in the workbook

Sub namerange()
x = Cells(Rows.Count, "a").End(xlUp).Row
Sheet1.Range("a1:a" & x).Name = "myname"
End Sub
 

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

Back
Top