Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
hdf hdf is offline
external usenet poster
 
Posts: 30
Default Delete range names within a selection

I've been searching extensively and can't seem to find something that
works, so I will try here.

I need to develop vba code that will delete all cell names within a
selected range. The selected range will include cells that are named
and cells that are not named.

Any help will be greatly appreciated.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Delete range names within a selection

hdf presented the following explanation :
I've been searching extensively and can't seem to find something that
works, so I will try here.

I need to develop vba code that will delete all cell names within a
selected range. The selected range will include cells that are named
and cells that are not named.

Any help will be greatly appreciated.


Have a look at the Intersect() function in VBA help.

Example...
<aircode
Dim rngname As Variant
' For Each rngname In ActiveWorkbook.Names '//use for global scope
For Each rngname In ActiveSheet.Names '//use for local scope
If Not Intersect(Selection, Range(rngname)) Is Nothing Then
'..delete the name
' ActiveWorkbook.Names(rngname).Delete '//use for global scope
ActiveSheet.Names(rngname).Delete '//use for local scope
End If
Next 'rngname

...where this will work if all or part of a named range falls within the
selection.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Delete range names within a selection

Oops! See the corrections below...

Example...
<aircode
Dim rngname As Variant
' For Each rngname In ActiveWorkbook.Names '//use for global scope
For Each rngname In ActiveSheet.Names '//use for local scope
If Not Intersect(Selection, Range(rngname)) Is Nothing Then
'..delete the name


' ActiveWorkbook.Names(rngname.Name).Delete '//for global scope
ActiveSheet.Names(rngname.Name).Delete '//for local scope

End If
Next 'rngname

..where this will work if all or part of a named range falls within the
selection.


--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #4   Report Post  
Posted to microsoft.public.excel.programming
hdf hdf is offline
external usenet poster
 
Posts: 30
Default Delete range names within a selection

Garry,

Thanks for the help. I was trying your first version and couldn't get
it to work, thanks for correcting it. I will now try and work with the
revised version.

HDF
  #5   Report Post  
Posted to microsoft.public.excel.programming
hdf hdf is offline
external usenet poster
 
Posts: 30
Default Delete range names within a selection

Well, I have not been able to get Garry's recommendations to work. I
am clearly not setting it up with all of the correct parameters.

However, I did find some code in another thread and now I have the
problem that it works just fine when testing it on a newly created
workbook, but for the life of me I can't get the exact same code to
work in my existing workbook.

I keep getting a 1004 Run-time error and it stops at the following
line of code: " If WithinRange(myCell, myName.RefersToRange) Then"

Here is the code:

Sub DeleteNames()

Dim myRange As Range
Dim myCell As Range
Dim myName As Name

Set myRange = Range("TESTRANGE")
For Each myCell In myRange.Cells
For Each myName In ThisWorkbook.Names
If WithinRange(myCell, myName.RefersToRange) Then
myName.Delete
End If
Next myName
Next myCell

End Sub
-------
Function WithinRange(SmallRng, BigRng) As Boolean
' Returns True if smallrng is a subset of Bigrng
WithinRange = False
If SmallRng.Parent.Name = BigRng.Parent.Name Then
If Union(SmallRng, BigRng).Address = BigRng.Address Then
WithinRange = True
End If
End If
End Function


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Delete range names within a selection

hdf used his keyboard to write :
Well, I have not been able to get Garry's recommendations to work. I
am clearly not setting it up with all of the correct parameters


All the correct parameters are there. I tested this with both local
scope named ranges and global scope named ranges. It worked perfectly
for me and so the only reason it may not be working for you is because
you're running it as posted, but your named ranges are global scope NOT
local scope. In this case, comment out the lines that act on
ActiveSheet and uncomment the lines that act on ActiveWorkbook.
(Commented lines are the ones prefixed with an apostrophe. Remove the
apostrophe from the global scope line and prefix the the local scope
line with an apostrophe!)

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Delete range names within a selection

Note that there are 2 lines each for global/local scope names. You must
change both as shown here...

Dim rngname As Variant
For Each rngname In ActiveWorkbook.Names '//use for global scope
' For Each rngname In ActiveSheet.Names '//use for local scope
If Not Intersect(Selection, Range(rngname)) Is Nothing Then
'..delete the name
ActiveWorkbook.Names(rngname.Name).Delete '//for global scope
' ActiveSheet.Names(rngname.Name).Delete '//for local scope
End If
Next 'rngname

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


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
How to Delete blanks between a range and populate only the names inthe given range Yuvraj Excel Discussion (Misc queries) 2 November 4th 09 08:32 PM
Delete all Range Names Q Seanie Excel Programming 5 January 11th 09 08:35 PM
Including ActiveX Controls in Names Range Selection Jack Excel Programming 6 March 12th 08 03:52 PM
Delete all Range Names Except for 1 Q Sean Excel Programming 2 January 16th 07 04:27 PM
delete non-used range names TxRaistlin Excel Programming 3 July 19th 06 09:05 AM


All times are GMT +1. The time now is 09:28 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"