+ Reply to Thread
Results 1 to 8 of 8

how to delete "NOT VALID" range names by vba

Hybrid View

bagullo how to delete "NOT VALID"... 06-02-2011, 09:29 AM
Whizbang Re: how to delete "NOT VALID"... 06-02-2011, 10:28 AM
bagullo Re: how to delete "NOT VALID"... 06-02-2011, 10:40 AM
Whizbang Re: how to delete "NOT VALID"... 06-02-2011, 11:39 AM
dvb_24 Re: how to delete "NOT VALID"... 06-02-2011, 11:57 AM
Whizbang Re: how to delete "NOT VALID"... 06-02-2011, 12:17 PM
bagullo Re: how to delete "NOT VALID"... 06-03-2011, 09:06 AM
Whizbang Re: how to delete "NOT VALID"... 06-03-2011, 12:52 PM
  1. #1
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2010
    Posts
    274

    Red face how to delete "NOT VALID" range names by vba

    Hi,

    I have to work sometimes with files that for some reason have a huge number of rangenames that refer to workbooks no longer there, and not only that, the range name itsef contains japanese characters, some of the references are dead etc.

    since these range names are useless (and sometimes do not allow to change to A1 reference mode) I want to delete them, however the normal sub to kill them (such as these do not delete these invalid names

    any workarounds?

    otherwise I'll have to delete them one by one, which works flawlessly, and endlessly...

    thanks!

  2. #2
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: how to delete "NOT VALID" range names by vba

    If the named range is referring to another workbook, the linked code will not work. This is because the linked code is looking for named range with #REF! in the address. #REF! only appears, as far as I know, if the named range references cells within the same workbook. This is because when these cells are deleted, Excel tries to update the named ranges, and cannot. But, if the named range refers to other workbooks, the current workbook has no way to determine if the cells exist in the other workbook, or even if the workbook exists at all.

    The only thing I can think of would be to add to the linked code a script that searches for the file, if the named range refers to another workbook, and if it does not exist, it deletes the named range. This will not handle events where the workbook does still exist, but that the cells referenced have been moved or deleted. This is because A1 (or whatever cell referenced) will always be there, even if the original A1 referenced was deleted or moved.

    So, in short, I do not think you will ever find a perfect solution that does not include human interaction.

    Here is the linked code modified to delete named ranges that refer to workbooks that do not exist:
    Sub DeleteDeadNames()
    
    Dim nName As Name
    Dim strFullPath As String
    
        For Each nName In Names
    
            If InStr(1, nName.RefersTo, "#REF!") > 0 Then
    
                nName.Delete
    
            End If
            
            If InStr(1, nName.RefersTo, "[") Then
            
                strFullPath = Replace(Mid(nName.RefersTo, 3, InStr(1, nName.RefersTo, "]") - 3), "[", "")
                
                If Dir(strFullPath, vbDirectory) = vbNullString Then
                    nName.Delete
                End If
            
            End If
    
        Next nName
    
    End Sub
    Last edited by Whizbang; 06-02-2011 at 10:30 AM. Reason: Stupid typos

  3. #3
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2010
    Posts
    274

    Re: how to delete "NOT VALID" range names by vba

    Quote Originally Posted by Whizbang View Post
    If the named range is referring to another workbook, the linked code will not work. This is because the linked code is looking for named range with #REF! in the address. #REF! only appears, as far as I know, if the named range references cells within the same workbook. This is because when these cells are deleted, Excel tries to update the named ranges, and cannot. But, if the named range refers to other workbooks, the current workbook has no way to determine if the cells exist in the other workbook, or even if the workbook exists at all.

    The only thing I can think of would be to add to the linked code a script that searches for the file, if the named range refers to another workbook, and if it does not exist, it deletes the named range. This will not handle events where the workbook does still exist, but that the cells referenced have been moved or deleted. This is because A1 (or whatever cell referenced) will always be there, even if the original A1 referenced was deleted or moved.

    So, in short, I do not think you will ever find a perfect solution that does not include human interaction.

    Here is the linked code modified to delete named ranges that refer to workbooks that do not exist:
    Sub DeleteDeadNames()
    
    Dim nName As Name
    Dim strFullPath As String
    
        For Each nName In Names
    
            If InStr(1, nName.RefersTo, "#REF!") > 0 Then
    
                nName.Delete
    
            End If
            
            If InStr(1, nName.RefersTo, "[") Then
            
                strFullPath = Replace(Mid(nName.RefersTo, 3, InStr(1, nName.RefersTo, "]") - 3), "[", "")
                
                If Dir(strFullPath, vbDirectory) = vbNullString Then
                    nName.Delete
                End If
            
            End If
    
        Next nName
    
    End Sub
    Wow, thanks a lot, I'll keep it in mind!
    I admit at the end I followed the good old way of clicking on a range name and then on delete very fast --- I just hope I do not come across to many files with the problem !

    regards

    Bernat

  4. #4
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: how to delete "NOT VALID" range names by vba

    I just realized that there is a way to test if a sheet exists in the other workbook.

    Sub DeleteDeadNames()
    
    Dim nName As Name
    Dim strRefersTo As String
    Dim strFullPath As String
    
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
    
        For Each nName In Names
        
            strRefersTo = nName.RefersTo
    
            If InStr(1, strRefersTo, "#REF") > 0 Then
    
                nName.Delete
    
            ElseIf InStr(1, strRefersTo, "[") Then
            
                strFullPath = Replace(Mid(strRefersTo, 3, InStr(1, strRefersTo, "]") - 3), "[", "")
                
                If Dir(strFullPath, vbDirectory) = vbNullString Then
                
                    nName.Delete
                    
                Else
                    Workbooks.Open Filename:=strFullPath, ReadOnly:=True
                    
                    If InStr(1, strRefersTo, "#REF") > 0 Then
                    
                        nName.Delete
                        
                    End If
                    
                    Workbooks(Mid(strRefersTo, InStr(1, strRefersTo, "[") + 1, InStr(1, strRefersTo, "]") - (InStr(1, strRefersTo, "[") + 1))).Close
                    
                End If
                
            End If
            
        Next nName
        
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    
    End Sub
    Last edited by Whizbang; 06-02-2011 at 12:17 PM.

  5. #5
    Registered User
    Join Date
    04-30-2009
    Location
    Spain
    MS-Off Ver
    Excel 2010
    Posts
    88

    Re: how to delete "NOT VALID" range names by vba

    Many thanks Whizbang I knew about the REF solution but was looking for something like this as many range names get mixed when working with different workbooks

  6. #6
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: how to delete "NOT VALID" range names by vba

    I just updated my most recent code sample. I realized there was a potential that it could try to delete the same named range twice (if the named range referenced another workbook, but also had #REF in the address). So, I changed it to an Elseif scenario.

  7. #7
    Forum Contributor
    Join Date
    09-30-2009
    Location
    Barcelona
    MS-Off Ver
    Excel 2010
    Posts
    274

    Thumbs up Re: how to delete "NOT VALID" range names by vba

    Quote Originally Posted by Whizbang View Post
    I just updated my most recent code sample. I realized there was a potential that it could try to delete the same named range twice (if the named range referenced another workbook, but also had #REF in the address). So, I changed it to an Elseif scenario.
    Thanks again so much.

    however my real problem is not so much to choose which range names to delete or not (would be happy to delete them all!), but rather than when the macro gets to the line rgnm.delete produces an error. also sometimes there are a number of hidden rangenames that do not appear even if I go through all range names and set rgnm.visible = true

    still the problem is small enough (sort of) for the good old contingency plan of doing it by hand!


    regards,

    Bernat

  8. #8
    Forum Expert Whizbang's Avatar
    Join Date
    08-05-2009
    Location
    Greenville, NH
    MS-Off Ver
    2010
    Posts
    1,395

    Re: how to delete "NOT VALID" range names by vba

    Please explain and clarify the error you are receiving. What is the error you are getting? Are some names being deleted, or are none? Post a sample workbook if you can.

    The code below will delete all named ranges:
    Sub DeleteAllNames()
    
    Dim nName As Name
    
        For Each nName In Names
    
           nName.Delete
    
        Next nName
    
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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