Hello
I am trying to delete ALL range names from WS. I recorded macro but I have a hard time to convert it to delete all of then at once.
Could you please help me with this?
Sub DelNames()
Dim i As Integer
For i = ActiveWorkbook.Names.Count To 1 Step -1
ActiveWorkbook.Names(i).Delete
Next i
End Sub
You have to step "backwards" through the names collection, otherwise when you delete '.names(1)' all the index referencing changes - there are now only N-1 names to delete, and what used to be the '.names(2)' range is now '.names(1)' - and will not be deleted! Trying to iterate through from 1 to 'names.count' will leave you with a subscript error, and ~half of the names you were trying to delete still in place.
Okay - I thought about it a bit, and you can get to the same place with:
Code:
Sub DelNames2()
Dim Nm As Name
For Each Nm In ActiveWorkbook.Names
Nm.Delete
Next Nm
End Sub
Obviously this avoids the issue of the changing index entirely, although that is still an issue to keep in mind - for instance, when iterating through rows in a worksheet...
We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel
Which adblocker are you using?
Disable AdBlock
Follow these easy steps to disable AdBlock
1)Click on the icon in the browser’s toolbar. 2)Click on the icon in the browser’s toolbar. 2)Click on the "Pause on this site" option.
Go back
Disable AdBlock Plus
Follow these easy steps to disable AdBlock Plus
1)Click on the icon in the browser’s toolbar. 2)Click on the toggle to disable it for "mrexcel.com".
Go back
Disable uBlock Origin
Follow these easy steps to disable uBlock Origin
1)Click on the icon in the browser’s toolbar. 2)Click on the "Power" button. 3)Click on the "Refresh" button.
Go back
Disable uBlock
Follow these easy steps to disable uBlock
1)Click on the icon in the browser’s toolbar. 2)Click on the "Power" button. 3)Click on the "Refresh" button.