+ Reply to Thread
Results 1 to 3 of 3

Delete nearly all defined names from the active worksheet only

Hybrid View

  1. #1
    Registered User
    Join Date
    08-31-2012
    Location
    Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    6

    Delete nearly all defined names from the active worksheet only

    Excel 2007

    Need to delete nearly all defined names from the active worksheet only. Worksheets can be named anything.

    These two worksheets must be excluded from having any names deleted:

    .codename(“Sheet1”)
    .name(“Summary”)

    Also, I don’t want any ~system names deleted, so exclude:
    (although Print_Area is the only one I’ve seen…)

    *_FilterDatabase
    *Print_Area
    *Print_Titles
    *wvu.*
    *wrn.*
    *!Criteria

    I have been trying to get something like this working (from searching these and other forums):

    Sub deleteinvalidnames()
    
    Dim invalidname As name
    Dim Ans As Integer
    Dim Msg As String
     
    For Each invalidname In ActiveWorkbook.Names
         If invalidname.RefersToRange.Worksheet.name = ActiveSheet And _
             Not invalidname.name Like "*Print_Area" And _
             ActiveSheet.CodeName <> "Sheet1" And _
             ActiveSheet.name <> "Summary" Then
                  Msg = invalidname.name & " = " & invalidname.RefersToRange.Address
                  Ans = MsgBox("Do you want to Delete/Rename: " & Msg, vbYesNo + vbInformation)
                  If Ans = vbYes Then invalidname.Delete
         End If
      Next invalidname
     
    ‘Then run other macro to create new names (this has no issues)…
    resetnames
     
    End Sub
    I’ve tried some mixes with:

    Set ws = Activesheet
    With ws
       For each invalidname in ws.names
    etc. but nothing seems to work.

    I don’t really need to cycle through with each name, but I came across that code first and it looked like a good thing to have in while troubleshooting…

    http://www.mrexcel.com/forum/excel-q...heet-only.html

    I would appreciate any advice as to how to proceed. Thank you.
    Last edited by JBeaucaire; 09-15-2013 at 08:16 PM. Reason: Added CODE tags, as per Forum Rules. Take a moment to read the Forum Rules in the menu bar above. Thanks.

  2. #2
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,784

    Re: Delete nearly all defined names from the active worksheet only

    Using the Name quality you get the sheet name and the range address of the particulart name so a macro like this could be used.


    Option Explicit
    
    Sub DelName()
    Dim nm As Name
    
    For Each nm In Names
    
    If InStr(nm, "Sheet1") = 0 Then
        nm.Delete
    End If
    
    Next nm
    
    End Sub
    will delete all range names on all sheets in a workbook except for the range names on sheet1
    Alf
    Last edited by Alf; 09-15-2013 at 03:17 AM. Reason: First answer was totaly wrong!!

  3. #3
    Registered User
    Join Date
    08-31-2012
    Location
    Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Delete nearly all defined names from the active worksheet only

    Quote Originally Posted by Alf View Post
    Using the Name quality you get the sheet name and the range address of the particulart name so a macro like this could be used.
    ...

    will delete all range names on all sheets in a workbook except for the range names on sheet1
    Alf

    Thanks. I'll see if I can get that to work with ActiveSheet tomorrow.


    What i really need help with is restricting the deletion of Names to the active worksheet (that can be named anything). I have a test for the two sheets i need to exclude, and should be able to avoid deleting Print_Area, etc. but i cannot get the Sub to restrict itself to the active sheet.
    Last edited by kjjjjshab; 09-17-2013 at 08:25 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 04-04-2010, 04:15 AM
  2. [SOLVED] delete defined names
    By Todd in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-06-2006, 04:15 PM
  3. How to delete all defined names from a workbook?
    By Dmitry Kopnichev in forum Excel General
    Replies: 15
    Last Post: 11-14-2005, 11:30 AM
  4. How to delete all defined names from a workbook?
    By Dmitry Kopnichev in forum Excel Formulas & Functions
    Replies: 15
    Last Post: 11-14-2005, 11:30 AM
  5. Macro to delete Defined Names
    By Mike Piazza in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-13-2005, 02:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1