Solved

Selecting all named ranges in one go using VBA

Posted on 2012-03-28
8
229 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 500 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

630 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