vba 遍历名称并删除那些与指定模式不匹配的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15339057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:57:36  来源:igfitidea点击:

loop through names and delete those not matching specified pattern

excel-vbaforeachnamed-rangesvbaexcel

提问by SimonJensen

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.

我有一个 Excel 工作簿,我在其中从其他几个工作簿导入工作表,然后将这些工作簿中的数据合并到“概述”工作表中。我对 vba 相当陌生,所以这项任务花了很长时间,并进行了大量研究。但是,我有一个无法解决或找不到答案的问题,但我认为如果您知道如何解决,那么解决起来应该相当简单。(所以希望有人这样做。) 问题:当我导入工作表时,我也导入了很多不需要的命名范围。我试图通过运行一个删除它们而不删除我指定的宏的宏来删除它们,但它对我不起作用。

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

回答by Our Man in Bananas

You need to use the namelocalproperty of the nameobject.

您需要使用name对象的namelocal属性。

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