Solved

Selecting all named ranges in one go using VBA

Posted on 2012-03-28
8
224 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
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:kgerb
Comment Utility
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
Comment Utility
Are they all on the same sheet? What do you want to do to them?
0
 

Author Comment

by:AndreasHermle
Comment Utility
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
 
LVL 12

Expert Comment

by:kgerb
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:AndreasHermle
Comment Utility
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
Comment Utility
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
Comment Utility
I am away on a business trip. Will test it shortly.
0
 

Author Closing Comment

by:AndreasHermle
Comment Utility
Hi kgerb,

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

Regards, Andreas
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now