Question

I have a Excel workbook in which I import sheets from several other workbooks and then merge the data from these into an "overview" sheet. I am fairly new to vba so this task has taken quite some time, and alot of research. However I have one problem that i cant solve or find an answer to, but i think it should be fairly simple to solve if you know how. (so hopefully someone out there do.) Problem: When i import the sheets i also import a lot of unwanted named ranges. I have attempted to remove these by running a macro that deletes them without deleting those i specify, but it dosnt work for me.

Any help is much appreciated

Sub DeleteNames()

Dim myName As Name

 For Each myName In ThisWorkbook.Names

  If myName <> "Tower" And _
  myName <> "Bird" Then
  myName.Delete
  End If

 Next
End Sub
Was it helpful?

Solution

You need to use the namelocal property of the name object.

Try this:

Sub DeleteNames()

Dim myName As Name

 For Each myName In ThisWorkbook.Names

  If myName.NameLocal <> "Tower" And myName.NameLocal <> "Bird" Then
    myName.Delete
  End If

 Next
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top