且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除命名范围时出现运行时错误

更新时间:2022-04-29 09:16:11

答案:问题是工作簿使用工作表名称作为命名范围的Name属性的一部分.具体来说,它使用的是history!history作为名称.

Answer: The problem was that the Workbook was using a worksheet name as part of the Name attribute of the named range. Specifically it was using history!history for the name.

故障排除方法:我使用了以下代码,该代码已发布到

Method of Troubleshooting: I used the following code that had been posted to a similar question at http://www.ozgrid.com/forum/showthread.php?t=49079&page=2

Dim nameRng As Name 
Dim varVal As Variant 
On Error Resume Next 
For Each nameRng In ActiveWorkbook.Names 
    varVal = Range(nameRng.Name).Value 
    nameRng.Delete 
Next

VBA编辑器中的本地窗口"显示该变量的nameRng.Name是字符串"history!history".

The Locals Window in the VBA Editor revealed that the nameRng.Name for this variable was the string "history!history".

修订后的代码:我删除了Application.Goto Reference:="history"行,因为它本质上是非功能性的代码行(类似于Select动作),并保留为删除导入范围名称的代码:

Revised code: I removed the Application.Goto Reference:="history" line since it was essentially a non-functional line of code (similar to a Select action) and was left with this as the code to delete the Name of the Imported range:

ActiveWorkbook.Names("history!history").Delete

平台:我在Windows 7 Professional上使用Excel 2013

Platform: I was using Excel 2013 on Windows 7 Professional