Solved

Delete a name on a sheet

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

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…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now