Solved

Delete a name on a sheet

Posted on 2011-09-16
4
184 Views
Last Modified: 2012-05-12
I have a name that is screwing up a macro.  Sometimes it exists and other times it does not.

How would I tell Excel, to only delete this if it exists on the sheet, if it does not exist continue. -R-
ActiveSheet.Names("Format").Delete

Open in new window

0
Comment
Question by:RWayneH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 167 total points
ID: 36552199
this will clear all names from the excel
Sub NoNames()
  For Each n In ActiveWorkbook.Names
    n.Delete
  Next n
End Sub

Open in new window

0
 
LVL 24

Assisted Solution

by:StephenJR
StephenJR earned 167 total points
ID: 36552291
To just delete that name, perhaps this:
On Error Resume Next
ActiveWorkbook.Names("Format").Delete
on error goto 0

Open in new window

0
 
LVL 2

Assisted Solution

by:jan24
jan24 earned 166 total points
ID: 36553583
StephenJR is right: there is no way to test for existence of an object in a collection, so you just have to use On Error Resume Next.
By the way, you probably already know this, but bear in mind that in Excel there are names which exist in the whole workbook - ActiveWorkbook.Names, and there are names which only exist in a sheet ActiveWorksheet.Names.  You might want to delete the name from both collections just to be on the safe side.  Modifying StephenJR's code to achieve this:
On Error Resume Next
ActiveWorkbook.Names("Format").Delete
ActiveSheet.Names("Format").Delete
on error goto 0 

Open in new window

0
 

Author Closing Comment

by:RWayneH
ID: 36576716
Thanks for the help. -R-
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question