Bright01
asked on
Leveraging Range Names in Excel Macros
EE Pros,
I will preface this as perhaps a strange question;
I often have a situation where I build a model that when I add a row or column, it screws up the Macro. When I use range names rather than cell references in a macro, it generally works out much better. With that said, how do quickly set up individual, non-contiguous cell references as range names (right now, this process is very laborious) and then now do I reference them in a Macro without listing each range name?
Thank you in advance,
B.
I will preface this as perhaps a strange question;
I often have a situation where I build a model that when I add a row or column, it screws up the Macro. When I use range names rather than cell references in a macro, it generally works out much better. With that said, how do quickly set up individual, non-contiguous cell references as range names (right now, this process is very laborious) and then now do I reference them in a Macro without listing each range name?
Thank you in advance,
B.
Can you give an example of your code that causes problems when you add a row. I think it would be better to address this problem first.
I am not sure I understand your question "How do I reference them in a Macro without listing each range name" ?
You have to either use the name or iterate through the 'workbook,names' or 'worksheet.names' collections
You have to either use the name or iterate through the 'workbook,names' or 'worksheet.names' collections
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
So Martin, You still have to name all the ranges.....even if they are single cells, but you can reference them in the Macro as a "group"? Even if they are non-contiguous? Right? Then, even if you move them around by say adding a column or row, they will auto adapt?
Roy, it's not a problem as much as it is in understanding how I may avoid one in the future.
Thanks,
B.
Roy, it's not a problem as much as it is in understanding how I may avoid one in the future.
Thanks,
B.
Yes to everything you asked except that the "move them around part". My code deals only with using existing ranges.
ASKER
Right. But even with your code, your code doesn't care WHERE the Ranges (i.e. cells) are in the WS.... and performs the operations on them because they are named and not specific addresses.
Do I have that right?
B.
Do I have that right?
B.
That's correct.
ASKER
Thank you!
Please don't forget to assign points.
ASKER
Martin,
Thanks for the lesson and the sample code! Big help.
B.
Thanks for the lesson and the sample code! Big help.
B.
You're welcome and I'm glad I was able to help.
In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014
In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014