Delete Reference not working?

Joe Was

MrExcel MVP
Joined
Feb 19, 2002
Messages
7,539
The code below removes broken references from a workbooks VBAProject-References. From its workbook it opens the workbook you want to check and deletes any broken references.

The actual code to delete the broken reference runs, but when you check that workbooks References all the removed ones are still listed with check boxes?

I must be doing something wrong?

Can someone run this code and see if you can fix it?
You can run it against a blank dummy workbook that you name in the code below, as long as it has some References selected.
Thanks, JSW.
P.S. I may be off on this and it may only remove the reference wile the two workbooks are open, but not do it permanently?

Sub DelBrokeRef()
'Check a WorkBook for Broken References and
'Delete any that are Broken!

'Run code from its own WorkBook, from a Standard Module.

'You must have a "Reference" in this WorkBook made to:
'Microsoft Visual Basic for Applications Extensibility 5.3 library
'to run this code!

'Add the name of the workbook you want to check, below!

Dim myRefs As VBIDE.Reference
Dim myProj As VBIDE.VBProject
Dim myWBook As String
Dim myCount
Dim myFile As String

On Error GoTo myError
myCount = 0

'Add the name of the workbook you want to check, below:
myFile = "ReferenceCheck.xls"

Workbooks.Open Filename:=myFile
myWBook = Workbooks(myFile).Name

MsgBox myWBook & " Opened for Checking" & Chr(13) & _
"and will now be checked for Broken References!"

Set myProj = Workbooks(myWBook).VBProject

For Each myRefs In myProj.References
If myRefs.IsBroken Then myProj.References.Remove myRefs
MsgBox myRefs.Name & Chr(13) & " Has been found to be Broken!" & _
Chr(13) & Chr(13) & myRefs.Name & Chr(13) & "Has been Removed!"
myCount = myCount + 1
Next myRefs

If myCount > 0 Then
MsgBox "Done Checking for Broken References!" & Chr(13) & _
myCount & ": References Found Broken!"
Else
MsgBox "No Broken References found, Done!"
End If
Exit Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Joe,

Apart from a problem with the way your If Then statement is written, this seems to work fine. Can you use this and see if you're still having a problem?

Code:
Sub DelBrokeRef()
'Check a WorkBook for Broken References and
'Delete any that are Broken!

'Run code from its own WorkBook, from a Standard Module.

'You must have a "Reference" in this WorkBook made to:
'Microsoft Visual Basic for Applications Extensibility 5.3 library
'to run this code!

'Add the name of the workbook you want to check, below!

    Dim myRefs As VBIDE.Reference
    Dim myProj As VBIDE.VBProject
    Dim myWBook As String
    Dim myCount
    Dim myFile As String

    On Error GoTo myError
    myCount = 0

    'Add the name of the workbook you want to check, below:
    myFile = "ReferenceCheck.xls"

    Workbooks.Open Filename:=myFile
    myWBook = Workbooks(myFile).Name

    MsgBox myWBook & " Opened for Checking" & Chr(13) & _
           "and will now be checked for Broken References!"

    Set myProj = Workbooks(myWBook).VBProject

    For Each myRefs In myProj.References
        If myRefs.IsBroken Then
            myProj.References.Remove myRefs
            MsgBox myRefs.Name & Chr(13) & " Has been found to be Broken!" & _
                   Chr(13) & Chr(13) & myRefs.Name & Chr(13) & "Has been Removed!"
            myCount = myCount + 1
        End If

    Next myRefs

    If myCount > 0 Then
        MsgBox "Done Checking for Broken References!" & Chr(13) & _
               myCount & ": References Found Broken!"
    Else
        MsgBox "No Broken References found, Done!"
    End If
    Exit Sub

myError:
    MsgBox Err.Description, , "Error!"

End Sub
 
Upvote 0
Thanks dk,

I did not think a single line If statement would fail here, but it did. Your corrected version did not find any bad References and my version flaged every Reference as bad, but did not delete any. I need to find a WorkBook with some bad References to fully test this. Thanks, JSW

P.S. I am still looking for a way to test for References missing from a WorkBook, but needed?
Any ideas?
 
Upvote 0

Forum statistics

Threads
1,216,174
Messages
6,129,296
Members
449,498
Latest member
Lee_ray

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top