Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Selecting Named Ranges

I has a worksheet with approx 20 separate named ranges. I
wish to clear the contents of all these ranges through VBA
in the most efficient way.

Is there a way of selecting all the named ranges within a
specifc worksheet so that I can apply ClearContents to all
the ranges at once.

Thanks.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Selecting Named Ranges

I guess you could build a union of them if you had a list. If you don't,

Dim rng as Range
Dim rng1 as Range
Dim nm as Name
Dim sh as Worksheet
set sh = Worksheets(1)
for each nm in Thisworkbook.Names
set rng = nothing
on Error Resume Next
set rng = nm.ReferstoRange
On Error goto 0
if not rng is nothing then
if rng.parent.name = sh.name then
if rng1 is nothing then
set rng1 = rng
else
set rng1 = union(rng,rng1)
end if
End if
End if
Next
if not rng1 is nothing then
rng1.clearcontents
End if

--
Regards,
Tom Ogilvy

"Ian" wrote in message
...
I has a worksheet with approx 20 separate named ranges. I
wish to clear the contents of all these ranges through VBA
in the most efficient way.

Is there a way of selecting all the named ranges within a
specifc worksheet so that I can apply ClearContents to all
the ranges at once.

Thanks.




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 620
Default Selecting Named Ranges

Ian,

How about just

Sub ClearRange()
Dim rng As Range
Dim sh As Worksheet

Set rng = Range("bob", "alan")
Set sh = Worksheets("Sheet1")
If Not rng Is Nothing Then
If rng.Parent.Name = sh.Name Then
rng.ClearContents
End If
End If
End Sub

--
HTH

-------

Bob Phillips
... looking out across Poole Harbour to the Purbecks


"Ian" wrote in message
...
I has a worksheet with approx 20 separate named ranges. I
wish to clear the contents of all these ranges through VBA
in the most efficient way.

Is there a way of selecting all the named ranges within a
specifc worksheet so that I can apply ClearContents to all
the ranges at once.

Thanks.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Selecting Named Ranges

Hi,



I'm still slipping this morning. Here the correction:



Sub DelName()

Dim nmA As Name

For Each nmA In ActiveWorkbook.Names

If ActiveSheet.Name = Mid(nmA.RefersToLocal, 2,
InStr(nmA.RefersToLocal, "!") - 2) Then

nmA.RefersToRange.Value = ""

End If

Next nmA

End Sub


--
JP

http://www.solutionsvba.com


"Ian" wrote in message
...
I has a worksheet with approx 20 separate named ranges. I
wish to clear the contents of all these ranges through VBA
in the most efficient way.

Is there a way of selecting all the named ranges within a
specifc worksheet so that I can apply ClearContents to all
the ranges at once.

Thanks.




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 77
Default Selecting Named Ranges

Here's just another general idea...

Sub Demo()
Dim nme As Name
Const ClearNamesOnThisSheet As String = "Sheet1"

For Each nme In ActiveWorkbook.Names
If StrComp( _
nme.RefersToRange.Parent.Name, _
ClearNamesOnThisSheet, _
vbTextCompare) = 0 Then
Range(nme).ClearContents
End If
Next nme
End Sub


--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"Ian" wrote in message
...
I has a worksheet with approx 20 separate named ranges. I
wish to clear the contents of all these ranges through VBA
in the most efficient way.

Is there a way of selecting all the named ranges within a
specifc worksheet so that I can apply ClearContents to all
the ranges at once.

Thanks.






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Selecting Named Ranges

There is no guarantee that ReferstoRange will be defined for all names. If
you don't account for that, you can get an error.

Of course in this user's case, that may not be an issue.

--
Regards,
Tom Ogilvy

"Dana DeLouis" wrote in message
...
Here's just another general idea...

Sub Demo()
Dim nme As Name
Const ClearNamesOnThisSheet As String = "Sheet1"

For Each nme In ActiveWorkbook.Names
If StrComp( _
nme.RefersToRange.Parent.Name, _
ClearNamesOnThisSheet, _
vbTextCompare) = 0 Then
Range(nme).ClearContents
End If
Next nme
End Sub


--
Dana DeLouis
Using Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"Ian" wrote in message
...
I has a worksheet with approx 20 separate named ranges. I
wish to clear the contents of all these ranges through VBA
in the most efficient way.

Is there a way of selecting all the named ranges within a
specifc worksheet so that I can apply ClearContents to all
the ranges at once.

Thanks.






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index and named ranges selecting difficulty Bruce Tharp Excel Worksheet Functions 7 June 19th 07 02:49 AM
Selecting Multiple Columns in a Named Selection Griffithpt Excel Worksheet Functions 0 August 9th 06 10:54 PM
Like 123, allow named ranges, and print named ranges WP Excel Discussion (Misc queries) 1 April 8th 05 06:07 PM
Selecting Filtered Items from Named range Soniya Excel Programming 2 August 20th 03 10:59 AM
selecting ranges MDC Excel Programming 1 July 24th 03 03:34 PM


All times are GMT +1. The time now is 03:19 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"