Problem Deleting Named Range

ERLoft

Board Regular
Joined
Feb 24, 2006
Messages
180
I have a workbook that imports data from a .txt file. In the process, it auto creates a named range "ExternalData_1", then the next update is "ExternalData_2" and so on. Since data is imported 3x weekly, this could quickly escalate into a whole lot of unneeded named ranges. As such, I want to remove the named range as part of the update process.

I tried adding a simple line of code: ActiveWorkbook.Names("ExternalData_1").Delete - but this is giving a 1004 error. Strange considering I test recorded the manual deletion of the named range and that's exactly what recorded.

I tried a more complex solution and while this one runs, it also causes corruption in the file. The file works just fine during that session, but the next time it's opened, it will go into recovery mode, which loses virtually all of the conditional formatting for some reason. The code I tried is:

VBA Code:
Sub DelExtDataNR()

Dim EDName As Name

For Each EDName In Names
        If InStr(1, EDName.Name, "ExternalData_1") Then
            EDName.Delete
        End If
Next EDName
End Sub

I suspect that the corruption is somehow related to the fact that Names contains other references that touch on ExternalData_1. I tested this by replacing the EDName.Delete with a counter. Even though there is only one ExternalData_1 listed in the name manager, this code will return a counter of 6. I'm just not sure how to find out what the other 5 instances of ExternalData_1 occurring are.

Any help would be greatly appreciated. As is, we have to manually go into the Name manager weekly and delete the auto created ExternalData references...
 

ERLoft

Board Regular
Joined
Feb 24, 2006
Messages
180
Okay, the second example there did work. And it appears it doesn't result in file corruption.

VBA Code:
Sub DeleteED()

Dim EDName As Name

For Each nName In ActiveWorkbook.Names
    If Left(nName.Name, 17) = "Data!ExternalData" Then EDName.Delete
Next nName
    
End Sub

Thanks much for the help!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,278
Office Version
  1. 365
Platform
  1. Windows
Do you need to remove the named range?
 

Watch MrExcel Video

Forum statistics

Threads
1,129,395
Messages
5,636,057
Members
416,895
Latest member
SteveRandall

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top