Macro Beginner - Help Selecting / Naming Changing Range

datadrvn

Board Regular
Joined
Apr 7, 2005
Messages
86
I promised I've tried seaching before asking...I'm trying to select a range of data so I can assign it a range name. I often import data where the number of columns and rows change and not every cell has data present.

I use the command "Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select" to highlight the desired section, which adjusts to the changing data nicely. The problem comes when it's time to name the range, Excel records the activity as:

ActiveWorkbook.Names.Add Name:="test", RefersToR1C1:= _
"='CF Open Migr CQs - 2010-03-10b'!R1C1:R38C22"
ActiveWorkbook.Names("test").Comment = ""

Unfortunately, this limits the name to the range I had selected at the time. When I run the macro a second time, if the range is different, the macro doesn't care as it has the previous range.

Help is always appreciated.

Wayne
Excel 2007
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Give this a shot:

Code:
ActiveWorkbook.Names.Add Name:="test", RefersTo:=Range(Selection, ActiveCell.SpecialCells(xlLastCell))
 
Upvote 0
Wayne

You shouldn't need to use Select/Selection for this.

It's also a bit easier to name a range with something like this.
Code:
Worksheets("Sheet1").Range("A1:C10").Name = "test"
Obviously you would change the sheet name and range as needed.

If you can give some more information about the range you want to name then perhaps someone can post something more specific.:)
 
Upvote 0
Never ceases to amaze me how everyone is so helpful. Thank you...this worked perfectly. I never thought of incorporating the selection in the name definition. I tried selecting first and then naming, because that's how I do it with the UI. I have so much to learn.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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