如何通过Excel VBA中的脚本删除缺less的引用?

我有很多用户使用的excel。 其中一些没有安装所有的软件,因此缺less参考。 我正在试图删除所有无法在C:驱动器上find的引用的脚本。

我有/ Microsoft Excel对象/ ThisWorkbook中的代码,但不完全正常工作。 任何人都可以帮助我。

Sub TestRef() Dim REF As VBIDE.Reference Dim WB As Workbook Set WB = ThisWorkbook For Each REF In WB.VBProject.References If StrComp(Left(REF.FullPath, 1), "C", vbTextCompare) <> 0 Then DelRef Debug.Print REF.Name, REF.Description, REF.FullPath End If Next REF End Sub Sub DelRef() Dim REF As VBIDE.Reference Dim WB As Workbook Set WB = ThisWorkbook With ThisWorkbook.VBProject.References .Remove .Item("MSForms") End With End Sub 

缺less参考图像

不是你的variables名称,但我写了一段时间,我已经使用了一段时间来删除“失踪”引用:

 Dim theRef As Variant, i As Long ' loop through all References in VB Project For i = ThisWorkbook.VBProject.References.Count To 1 Step -1 Set theRef = ThisWorkbook.VBProject.References.Item(i) ' if reference is "Missing" >> remove it to avoid error message If theRef.isbroken = True Then ThisWorkbook.VBProject.References.Remove theRef End If ' just for Debug ' Debug.Print theRef.Description & ";" & theRef.FullPath & ";" & theRef.isbroken & vbCr Next i