Leveraging Range Names in Excel Macros

Bright01
Bright01 used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Roy CoxGroup Finance Manager

Commented:
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
Older than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
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

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

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 dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Yes to everything you asked except that the "move them around part".  My code deals only with using existing ranges.

Author

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 dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
That's correct.

Author

Commented:
Thank you!
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Please don't forget to assign points.

Author

Commented:
Martin,

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

B.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial