excel vba,从工作簿中删除所有名称而无需循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41970802/
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-12 12:00:50  来源:igfitidea点击:

excel vba, deleting all names from a workbook without a loop

excelvba

提问by Charteris

I'm trying to delete all names from an excel workbook using VBA without using a loop.

我正在尝试使用 VBA 从 Excel 工作簿中删除所有名称而不使用循环。

I'm currently using the code below, but this is very slow as there are several thousand names in the workbook.

我目前正在使用下面的代码,但这很慢,因为工作簿中有几千个名字。

Any suggestions would be appreciated!

任何建议,将不胜感激!

Sub deleteAllNames()
Dim xName As Name

For Each xName In Application.ActiveWorkbook.Names
       xName.Delete
Next
End Sub

回答by Slai

Not possible without some complicated hacky way or messing with the XML, but this should be faster:

没有一些复杂的hacky方式或弄乱XML是不可能的,但这应该更快:

Dim i As Long
Application.Calculation = xlCalculationManual
For i = ThisWorkbook.Names.Count To 1 Step -1
    ThisWorkbook.Names(i).Delete
Next
Application.Calculation = xlCalculationAutomatic

回答by matheus silveira

I had the same problem deleting all named ranges, the code was running very slowly. You can fix this by turning off the screen update and the calculation while the loop is running.

我在删除所有命名范围时遇到了同样的问题,代码运行非常缓慢。您可以通过在循环运行时关闭屏幕更新和计算来解决此问题。

Sub deleteAllNames()

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

Dim xName As Name

For Each xName In Application.ActiveWorkbook.Names
       xName.Delete
Next

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub