Solved

Delete a name on a sheet

Posted on 2011-09-16
4
183 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simplify construction of formula 4 58
need help to look for 16 42
sort time order 10 45
Excel Add-in Subscript out of range 5 29
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

734 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