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

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Zot

Well-known Member
Joined
Nov 26, 2020
Messages
1,151
Office Version
  1. 2016
Platform
  1. Windows
Try something like this (change interest sheet name accordingly). See if this works:
VBA Code:
Sub DelExtDataNR()

Dim EDName As Name

For Each EDName In ActiveWorkbook.Names
    If EDName.RefersToRange.Parent.Name = "Sheet1" Then
        EDName.Delete
    End If
Next EDName

End Sub
 

ERLoft

Board Regular
Joined
Feb 24, 2006
Messages
180
If EDName.RefersToRange.Parent.Name = "Data" Then

Creates 1004 error on run.
 

ERLoft

Board Regular
Joined
Feb 24, 2006
Messages
180

ADVERTISEMENT

That also produced a 1004. Seems as though it's tied to the name "ExternalData_1". I tried another named range and it all works fine. I thought maybe I had a typo, so I tried copy-paste on the range name from the name manager, but still getting a 1004 for "ExternalData_1". Maybe I'll try renaming the range, then deleting it?
 

ERLoft

Board Regular
Joined
Feb 24, 2006
Messages
180
Tried renaming "ExternalData_1" with another 1004 result. Something about the name range created by the text import causes issues when attempting to manipulate with VBA. Manual deletion with the name manager works fine though. Very strange.
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,776

ADVERTISEMENT

I think I've seen this before. When you imported this, it came as a table and the named range "ExternalData_1" was added to refer to the table. Perhaps, deleting the listobject (table) first and then deleting the named range would be possible? Dave
 

NdNoviceHlp

Well-known Member
Joined
Nov 9, 2002
Messages
2,776
I found the download that I have which has an "ExternalData_1" named range. There is no listobject even though the imported data seems like it's in table format. So no listobject to delete but I was able to delete the "ExternalData_1" named range with the original code that I posted?? Hmmm... sorry no further suggestions. Dave
 

Watch MrExcel Video

Forum statistics

Threads
1,129,530
Messages
5,636,861
Members
416,946
Latest member
mniceguy81

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