LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
hdf hdf is offline
external usenet poster
 
Posts: 30
Default Code to delete names works in one WB, but not another WB?

I was looking for code to delete cell names within a selected range.
I found the following code which works just fine in the WB I created
to test it, but then won't work in the existing workbook for which I
needed it. It keeps giving a 1004 Run-time error and stops at this
line: " If WithinRange(myCell, myName.RefersToRange) Then".

I have no idea why it works ok in my test WB but not the other one. I
have ensured cells and worksheet are not locked.

Here is the full code - where "TESTRANGE" is the range that contains
the named cells I want to erase. Not all cells within the range are
named.

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
 
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
using the Excel generic worksheet names instead of user-given names in code Paul Excel Discussion (Misc queries) 5 June 26th 09 08:44 PM
if by accident you delete ur microsoft works what do u do? Naomi Freeland Excel Discussion (Misc queries) 2 January 8th 08 04:36 PM
It works but I need better code... [email protected] Excel Programming 6 April 10th 07 01:29 PM
Code to Delete Range Names Sean Excel Programming 3 January 3rd 07 11:05 PM
Code somewhat works. Please help? jsc3489 Excel Programming 4 December 2nd 05 07:51 PM


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