Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Selecting all named ranges in one go using VBA

Posted on 2012-03-28
8
Medium Priority
?
232 Views
Last Modified: 2012-03-31
Dear experts:

I got a whole bunch of named ranges in my current workbook, named range01, range02, range03, range04, range05 and so forth. These are all workbook level scope ranges.

How can I select these ranges in one go using VBA?

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
0
Comment
Question by:AndreasHermle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:kgerb
ID: 37777208
I'm pretty sure you're not going to be able to perform an action on all named range in a single line.  However, you can loop through them and do something to them one at a time.  Here's an example.
Sub SelectAllNamedRangesOneAtATime()
Dim n As Name, sht As String
For Each n In ThisWorkbook.Names
    'Put your code here
    'For example
    sht = Replace(Mid(n, 2, InStr(1, n, "!") - 2), "'", "")
    If ActiveSheet.Name <> sht Then Sheets(sht).Activate
    Range(n).Select
Next n
End Sub

Open in new window

Kyle
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 37777247
Are they all on the same sheet? What do you want to do to them?
0
 

Author Comment

by:AndreasHermle
ID: 37777485
Hi rorya:

they, ie. the ranges are all on the same sheet. I would like to perform a search and replace action after they have all been selected.

Thank you.

HI Kyle:

I understand that I am to complete the code myself with a couple of lines (you wrote: 'Put your code here and 'for example). As a matter of fact I do not know how to complete it.

If I run your code without completing only the last named range of the ranges named range_01, range_02 and so forth gets selected.

Thank you,

Regards, Andreas a
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:kgerb
ID: 37777955
No problem, I can help you with the code.

You said you need to search for something in each of the named ranges and replace it with something else.  What are you searching for and what do you want to replace it with?

Kyle
0
 

Author Comment

by:AndreasHermle
ID: 37779229
Kyle:

I am  searching for blank cells in these ranges. The found cells are to be filled with a couple of spaces. May sound odd, but I need all cells filled somehow.

Thank you.

Regards, Andreas
0
 
LVL 12

Accepted Solution

by:
kgerb earned 2000 total points
ID: 37781514
Andreas,
Here is the code.  It loops through each cell in each named range.  If an empty cell is found it fills it with two spaces.  Let me know how it works.
Sub SelectAllNamedRangesOneAtATime()
Dim n As Name, r As Range, sht  As String
For Each n In ThisWorkbook.Names
    sht = Replace(Mid(n, 2, InStr(1, n, "!") - 2), "'", "")
    If ActiveSheet.Name <> sht Then Sheets(sht).Activate
    For Each r In Range(n)
        If r = "" Then r = "  "
    Next r
Next n
End Sub

Open in new window

Kyle
0
 

Author Comment

by:AndreasHermle
ID: 37782826
I am away on a business trip. Will test it shortly.
0
 

Author Closing Comment

by:AndreasHermle
ID: 37791836
Hi kgerb,

works just great! Thank you very much for  your professional help. I really appreciate it.

Regards, Andreas
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

661 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question