Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何以编程方式删除引用?_Excel_Vba - Fatal编程技术网

Excel 如何以编程方式删除引用?

Excel 如何以编程方式删除引用?,excel,vba,Excel,Vba,我已经使用Excel 2016作为Citrix应用程序编写了VBA代码,并按预期运行 然而,当我在Excel 2010中在普通桌面上运行此宏时,我面临一个参考问题。删除引用后,它将运行 我想在运行时使用VBA删除显示为“Missing:ALTEntityPicker 1.0类型库”的引用 我尝试了以下方法: Sub DeleteRef(RefName) Dim ref As Reference 'You need a reference to remove Set re

我已经使用Excel 2016作为Citrix应用程序编写了VBA代码,并按预期运行

然而,当我在Excel 2010中在普通桌面上运行此宏时,我面临一个参考问题。删除引用后,它将运行

我想在运行时使用VBA删除显示为“Missing:ALTEntityPicker 1.0类型库”的引用

我尝试了以下方法:

Sub DeleteRef(RefName)
    Dim ref As Reference

    'You need a reference to remove
    Set ref = References("Missing: ALTEntityPicker 1.0 Type Library")
    References.Remove ref
End Sub 
试试这个

Sub DeleteRef(RefName) 
    Dim ref As Reference

    'You need a reference to remove '
    Set ref = References("Missing: ALTEntityPicker 1.0 Type Library")
    vbProj.References.Remove ref
End Sub

在丢失发生后,仅在发生之前或发生之后手动删除丢失/断开的引用,不可能通过编程方式删除丢失/断开的引用。 大多数缺少/断开引用的情况都是因为类型库以前从未在该系统上注册过

预防:通过在关闭事件之前通过
Workbook\u删除任何有问题的引用,并将其重新添加到
Workbook\u Open
事件中,预先避免丢失/断开引用。在示例中,
“Selenium”
是一个导致错误的引用,因此我在关闭之前使用
工作簿将其删除,并将其添加回打开的
工作簿中。如果无法添加,则不会添加,也不会出现缺失,如果可以添加,则会添加




请看一看自动删除所有断开引用的方法。@BigBen-很抱歉,它不是chkRef,它的ref在这里(它只是一个引用变量)谢谢,我已经更改了另一个答案来回答这个问题。
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Cancel = True Then Exit Sub
    RemoveReference
End Sub
Private Sub Workbook_Open()
 AddReferences
End Sub
Public Sub RemoveReference()
On Error GoTo EH
    Dim RefName As String
    Dim ref As Reference
    RefName = "Selenium"
     
    Set ref = ThisWorkbook.VBProject.References(RefName)
    ThisWorkbook.VBProject.References.Remove ref
    
Exit Sub
EH:
'If an error was encountered, inform the user
    Select Case Err.Number
        Case Is = 9
            MsgBox "The reference is already removed"
        Exit Sub
        Case Is = 1004
            MsgBox "You probably do not have to have Trust Access To Visual Basic Project checked or macros enabled"
        Exit Sub
       Case Else
         'An unknown error was encountered
            MsgBox "Error in 'RemoveReference'" & vbCrLf & vbCrLf & Err.Description
    End Select
End Sub
Public Sub AddReferences()
    Dim wbk As Workbook
    Set wbk = ActiveWorkbook

    AddRef wbk, "{0277FC34-FD1B-4616-BB19-A9AABCAF2A70}", "Selenium"
End Sub

Sub AddRef(wbk As Workbook, sGuid As String, sRefName As String)
    Dim i As Byte
    On Error GoTo EH
    With wbk.VBProject.References
        For i = 1 To .Count
            If .item(i).Name = sRefName Then
               Exit For
            End If
        Next i
        If i > .Count Then
           .AddFromGuid sGuid, 0, 0 ' 0,0 should pick the latest version installed on the computer
        End If
    End With

Exit Sub

EH:
'If an error was encountered, inform the user
    Select Case Err.Number
        Case Is = 1004
            MsgBox "You probably do not have to have Trust Access To Visual Basic Project checked or macros enabled"
        Exit Sub
    Case Else
         'An unknown error was encountered
            MsgBox "Error in 'AddRef'" & vbCrLf & vbCrLf & Err.Description
    End Select
End Sub
Public Sub ExistingRefs()
 Dim i As Byte
 On Error GoTo EH
      With Application.ThisWorkbook.VBProject.References
        For i = 1 To .Count
            Debug.Print "    AddRef wbk, """ & .item(i).GUID & """, """ & .item(i).Name & """"
        Next i
    End With
    
Exit Sub
EH:
'If an error was encountered, inform the user
    Select Case Err.Number
        Case Is = 1004
            MsgBox "You probably do not have to have Trust Access To Visual Basic Project checked or macros enabled"
        Exit Sub
    Case Else
         'An unknown error was encountered
            MsgBox "Error in 'ExistingRefs'" & vbCrLf & Err.Description
    End Select
End Sub