loop through names and delete those not matching specified pattern

落爺英雄遲暮 提交于 2019-12-30 15:41:28

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/15339057/loop-through-names-and-delete-those-not-matching-specified-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!