简体   繁体   English

使用VBA在Excel中删除残破的命名范围

[英]Delete Broken Named Ranges in Excel Using VBA

Part of a much larger macro involves the detection and deletion of Named Ranges that were unintentionally duplicated upon the Move/Copy of a sheet to the main workbook. 更大的宏的一部分涉及检测和删除在将工作表移动/复制到主工作簿时无意中重复的命名范围。 These "broken" Named Ranges are indicated by the "Value" column in the Name Manager showing "#REF!". 这些“中断的”命名范围由名称管理器中的“值”列指示,显示为“ #REF!”。

I've attempted the deletion of these named ranges using the following macro: 我尝试使用以下宏删除这些命名范围:

Sub DeleteBrokenNamedRanges()
Dim NR As Name
Dim numberDeleted As Variant

numberDeleted = 0
For Each NR In ActiveWorkbook.Names
    If InStr(NR.Value, "#REF!") > 0 Then
        NR.Delete
        numberDeleted = numberDeleted + 1
    End If
Next

MsgBox ("A total of " & numberDeleted & " broken Named Ranges deleted!")

End Sub

Unfortunately, the return value is 0 and no Named Ranges were deleted. 不幸的是,返回值为0,并且没有删除命名范围。 I played around with protect/unprotect and some parameters of InStr , but nothing has worked. 我玩过保护/取消保护和InStr一些参数,但是没有任何效果。

Side Note - The return of NR.Value is not #REF! NR.Value的返回不是#REF! or similar error code as expected, but is actually the =C:\\Blahblah\\blarg.xls path instead. 或预期的类似错误代码,但实际上是=C:\\Blahblah\\blarg.xls路径。

Any help on this would be greatly appreciated, thanks! 任何帮助,将不胜感激,谢谢!

I don't think error codes exist as range values (not that I'm any authority on this). 我不认为错误代码作为范围值存在(不是我对此有任何权威)。 If valid ranges had errors other than #REF! 如果有效范围内的错误不是#REF! it could be a problem, but I think something like 这可能是一个问题,但我认为类似

IsError(NR.RefersToRange)

Might be a good way to check for broken named ranges? 可能是检查损坏的命名范围的好方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM