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

时间:2017-04-28 08:48:34

标签: excel vba excel-vba

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

我在/ Microsoft Excel Objects / 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   

Missing Referece Image

1 个答案:

答案 0 :(得分:1)

不是你的变量名称,而是我之前写过的东西,我已经使用了一段时间来删除&#34; Missing&#34;参考文献:

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