Delete thousand of named ranges in excel without getting #NAME? Error

123 Views Asked by At

I have an excel file which has ~85,000 named ranges. I want to delete these named ranges but do not want to impact any calculations. If I am deleting these named ranges, I am getting '#NAME?' error where these ranges were used. I am using below VBA code:

Sub deletename()

    Dim j As Long
    Dim i As Long
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    j = ActiveWorkbook.Names.Count
    
    For i = j To 1 Step -1
        If Not (InStr(ActiveWorkbook.Names(i).Value, "unit") > 0) Then
            ActiveWorkbook.Names(i).Delete
        End If
    Next i
    
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    
End Sub

Is there any way to change the formula to refer to the cell instead of named ranges before deleting them? If not, can I just hardcode the values before deleting the named ranges so I dont get '#NAME?' error?

Any help will be greatly appreciated Thanks.

I tried the VBA code mentioned above and was able to delete the named ranges but my excel is filled with '#NAME?' errors. Is there a way to delete named ranges without getting '#NAME?' errors?

0

There are 0 best solutions below