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.
Bright01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roy CoxGroup Finance ManagerCommented:
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.
AL_XResearchCommented:
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
Martin LissOlder than dirtCommented:
If the names of the named ranges that you want to use have something common, you can do the following. This example assumes that they start with "Test".
Sub x()
Dim NR As Name
Dim rng As Range

For Each NR In Names
    If Left$(NR.Name, 4) = "Test" Then
        If rng Is Nothing Then
            Set rng = Range(NR)
        Else
            Set rng = Union(rng, Range(NR))
        End If
    End If
Next

' do something with rng
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Bright01Author Commented:
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.
Martin LissOlder than dirtCommented:
Yes to everything you asked except that the "move them around part".  My code deals only with using existing ranges.
Bright01Author Commented:
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.
Martin LissOlder than dirtCommented:
That's correct.
Bright01Author Commented:
Thank you!
Martin LissOlder than dirtCommented:
Please don't forget to assign points.
Bright01Author Commented:
Martin,

Thanks for the lesson and the sample code!  Big help.

B.
Martin LissOlder than dirtCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.