Results 1 to 1 of 1

Thread: Delete Name Ranges by Scope VBA

  1. #1
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10

    Lightbulb Delete Name Ranges by Scope VBA

    Hi All,


    Here is a workaround which allows you to delete the name ranges from a workbook where both the scope exists of same name

    Code:
    Option Explicit
    Public Enum NameScope
        xlWorkbook = 0
        xlWorksheet = 1
    End Enum
    Sub DeleteNamedRanges(ByRef Wbk As Workbook, ScopeLevel As NameScope)
        
        '// Developed by    : Krishnakumar @ ExcelFox.com
        
        Dim lngLoop     As Long
        Dim lngIndex    As Long
        Dim strName     As String
        Dim wksTemp     As Worksheet
        Dim lngSU       As Long
        Dim lngCalc     As Long
        Dim lngEE       As Long
        Dim lngDA       As Long
        
        
        With Application
            lngSU = .ScreenUpdating
            lngCalc = .Calculation
            lngDA = .DisplayAlerts
            lngEE = .EnableEvents
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
            .DisplayAlerts = False
            .EnableEvents = False
        End With
        
        Set wksTemp = Wbk.Worksheets.Add
        
        With Wbk
            For lngLoop = .Names.Count To 1 Step -1
                If ScopeLevel = xlWorksheet Then
                    If TypeOf .Names(lngLoop).Parent Is Worksheet Then
                        strName = Split(.Names(lngLoop).Name, "!")(1)
                        If GLOBALLYEXISTS(Wbk, strName) Then
                            .Names(lngLoop).Delete
                        End If
                    End If
                ElseIf ScopeLevel = xlWorkbook Then
                    If TypeOf .Names(lngLoop).Parent Is Workbook Then
                        strName = "!" & .Names(lngLoop).Name
                        If LOCALLYEXISTS(Wbk, strName) Then
                            .Names(lngLoop).Delete
                        End If
                    End If
                End If
            Next
        End With
        
        wksTemp.Delete
        
        With Application
            .ScreenUpdating = lngSU
            .Calculation = lngCalc
            .DisplayAlerts = lngDA
            .EnableEvents = lngEE
        End With
    
    End Sub
    Private Function GLOBALLYEXISTS(ByRef Wbk As Workbook, ByVal NameName As String) As Boolean
        
        Dim lngLoop     As Long
        Dim lngSU       As Long
        
        With Application
            lngSU = .ScreenUpdating
            .ScreenUpdating = False
        End With
        
        With Wbk
            For lngLoop = .Names.Count To 1 Step -1
                If .Names(lngLoop).Name = NameName Then
                    If TypeOf .Names(lngLoop).Parent Is Workbook Then
                        GLOBALLYEXISTS = True
                        GoTo Xit
                    End If
                End If
            Next
        End With
    Xit:
        Application.ScreenUpdating = lngSU
        
    End Function
    Private Function LOCALLYEXISTS(ByRef Wbk As Workbook, ByVal NameName As String) As Boolean
        
        Dim lngLoop     As Long
        Dim lngSU       As Long
        
        With Application
            lngSU = .ScreenUpdating
            .ScreenUpdating = False
        End With
        
        With Wbk
            For lngLoop = .Names.Count To 1 Step -1
                If .Names(lngLoop).Name Like "*" & NameName Then
                    If TypeOf .Names(lngLoop).Parent Is Worksheet Then
                        LOCALLYEXISTS = True
                        GoTo Xit
                    End If
                End If
            Next
        End With
    Xit:
        Application.ScreenUpdating = lngSU
        
    End Function
    and call the procedure as

    Code:
    Sub kTest()
            
        'Delete Local Names where both the scope exists (Local and Global)
        DeleteNamedRanges ThisWorkbook, xlWorksheet
        
        'Delete Global Names where both the scope exists (Local and Global)
        'DeleteNamedRanges ThisWorkbook, xlWorkbook
        
    End Sub
    Last edited by Admin; 12-20-2011 at 05:56 AM. Reason: Modified code. Fixed couple of bugs
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2013, 04:59 PM
  2. Automatically Create Multiple Dynamic Named Ranges Using VBA
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 0
    Last Post: 04-24-2013, 03:49 PM
  3. Add Named Ranges To Multiple Workbooks Using VBA
    By Stalker in forum Excel Help
    Replies: 5
    Last Post: 04-23-2013, 12:20 PM
  4. Delete Remove Rows By Criteria VBA Excel
    By marreco in forum Excel Help
    Replies: 5
    Last Post: 12-20-2012, 05:56 PM
  5. List all the name ranges in an excel sheet with scope
    By LalitPandey87 in forum Excel Help
    Replies: 4
    Last Post: 03-28-2012, 07:27 AM

Tags for this Thread

Posting Permissions

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