Problem Deleting Named Range

ERLoft

Board Regular
Joined
Feb 24, 2006
Messages
193
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...
 
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!
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Do you need to remove the named range?
 
Upvote 0

Forum statistics

Threads
1,214,823
Messages
6,121,780
Members
449,049
Latest member
greyangel23

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
Back
Top