View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
Brian Murphy Brian Murphy is offline
external usenet poster
 
Posts: 126
Default trouble returning a workbook level Name object

I just tried your approach with the following loop, and o.Delete deleted the wrong name object, even though it was pointing to the correct book level name object! For this to occur "somename" needs to be sheet level on the first sheet, and book level on another sheet.

Sub test2()
Dim o
For Each o In ActiveWorkbook.Names
If o.Name = "somename" Then o.Delete
Next
End Sub


Brian



"Peter T" wrote in message ...
Brian,

This odd behavior becomes a real problem when trying
to do something like delete a book level name.


Do I take it you want to delete a book level name but keep
any/all sheet level names of the same name?

Trouble is Sheet names are both members both of the
worksheet.names collection and the workbook.names
collection. Maybe this might work:

Sub DelBookName()
Dim wk As Worksheet, nm As Name
Dim sName As String
sName = "somename"

For Each nm In ActiveWorkbook.Names

'if it's a sheet name should return "Sheetname!somename"
Debug.Print nm.Name

'does not include "Sheetname! so should be safe to delete
If nm.Name = sName Then nm.Delete
Next

End Sub

Regards,
Peter

<snip