+ Reply to Thread
Results 1 to 8 of 8

how to delete "NOT VALID" range names by vba

Hybrid View

  1. #1
    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

  2. #2
    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

+ 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