Odd numbered Range Names ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a macro program aimed at deleting unwanted range names.
The intent is for the program to cycle through each range name until it
finds one meeting my delete criteria, delete it then move on.
Suppose I have 10 range names (eg MyRange_1 thru MyRange_10), all meeting my
delete criteria, to remove.
The macro runs OK, but only selects and deletes EVEN numbered range names.
If I then run the program again, it removes numbers 1, 5 & 9, leaving 3 & 7.
Running again removes 3
Running again removes 7

I use "MyName = Names(X).Name" to cycle through,
where X is from "For X= 1 to 10 Step 1"

I feel there is something I don't understand about how Excel stores range
names.
How can I make my program delete all in one pass?
Any suggestions most welcome.
 
Hi Don,

Try reversing the sequence:

For X= 10 to 1 Step -1
If Your condition = True Then
 
Hi,

You have to delete in reverse order:

Sub DeleteRanges()


For i = Names.Count To 1 Step -1
If Left(Names(i).Name, 7) = "MyRange" Then <===== Change as required
Names(i).Delete
End If

Next i
End Sub


HTH
 
Thanks Norman & Toppers
I reversed the sequence & used T's snippet of code. It works fine.
 
Back
Top