hdf wrote on 4/29/2012 :
Thanks for the follow up.
The code is in a standard module. I have discovered that if I use the
code on a workbook with more than one WS and both worksheets have
names in them, it crashes.
I started with a fresh WB, named four consecutive cells in each of two
WS (different names for each cell). Then I placed a button linked to
the macro in each WS. I then select half of the named cells in either
one of the two WS and run the macro. It crashes.
If I start with a fresh WB and only name cells on one WS, select some
of them and run the macro, it works as intended.
Is there any reason why it would only work on a single WS workbook?
It works for the active sheet of whichever workbook is active at the
time the code runs. It doesn't matter how many sheets are in the
workbook because only 1 sheet can be active at any time.
What's important to make it work is whether the names are defined with
local or global scope. To make this easier for you, I've created 2
separate procedures so you can choose the scope befor you run the
code...
Sub DeleteGlobalScopeNames()
Dim rngname As Variant
For Each rngname In ActiveWorkbook.Names
If Not Intersect(Selection, Range(rngname)) Is Nothing Then
'..delete the name
ActiveWorkbook.Names(rngname.Name).Delete
End If
Next 'rngname
End Sub
Sub DeleteLocalScopeNames()
Dim rngname As Variant
For Each rngname In ActiveSheet.Names
If Not Intersect(Selection, Range(rngname)) Is Nothing Then
'..delete the name
ActiveSheet.Names(rngname.Name).Delete
End If
Next 'rngname
End Sub
I suggest you put this in PERSONAL.XLS so it's always available
whenever you have Excel open. If your PERSONAL.XLS file is not open (in
the VBE Explorer pane) then it doesn't exist yet. You can create one by
recording a macro and choosing PERSONAL.XLS during the process. You
don't actually have to record anything so just click 'Stop recording',
delete the empty macro in 'Module1', then paste these subs into that
module.
To run the macros from the Excel UI, open the 'Macros...' dialog:
In Ribbon versions of Excel it can be found on the 'Developer' tab.
In earlier versions of Excel it can be accessed via the 'Tools' menu.
--
Garry
Free usenet access at
http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion