Macro to Name a dynamic Row/Col range

Cheryl4g

New Member
Joined
Jan 11, 2011
Messages
29
There is a lot of info on dynamic ranges but I can't figure out how to put it together for what I need and hopefully someone can help me.

I have a spreadsheet with the headers on row 1 col 1 to col (whatever) and the data right below it. I need the macro to select all the data from row 1 down to the last row of data and all the data from the first column to the last column with a header and name the range "Data".

First column of data is usually text but it would be useful to know how to use it for number or text number combinations as well.

Thanks for the help,
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
If the table contains no blank rows or columns, try:

Code:
Range("A1").CurrentRegion.Select
ActiveWorkbook.Names.Add Name:="Data", RefersTo:=Selection

Denis
 
Upvote 0
What if the headers start on row 34 instead of row 1? Is there a way to alter the code to work for that or does that make it too complex?
 
Upvote 0
Change the first line. Change Range("A1") to Range("A34").

If the headers could be in any row except 1, and there is *nothing else* in Column A above the headers, you can use

Code:
Range("A1").End(xlDown).CurrentRegion.Select
ActiveWorkbook.Names.Add Name:="Data", RefersTo:=Selection

Denis
 
Upvote 0
Hi, I have a slight modification to this that I am having trouble with and would greatly appreciate your help.

I am trying to rename a range after an autofilter is applied through a macro. The column secetion is fixed, A:J, and the rows are variable. This is the code I have been trying, which gives me a compile error.

Code:
.Range(.Range("A3:J3"), _
.Range("A3").End(xlDown)).Name = "filtercriteria"
 
Upvote 0
Something like...

Code:
Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Select
ActiveWorkbook.Names.Add Name:="FilterData", RefersTo:=Selection

But if you recreate the name for another filter, you should delete the existing name first and then create the replacement name.

Denis
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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