Reference VBA tweaking

simico

New Member
Joined
May 27, 2011
Messages
30
Hey all,

Excel 2007 and Windows XP are my particulars.

I have VBA code that works very well, but it needs a little tweaking. I've tried several things myself, but keep getting errors.
The code checks to see if a reference exists; if it does, then it exits the sub. If it does not, it adds it and runs a macro.

Here's what I want it to do:
The code checks to see if a reference exists; if it does, runs a macro and then it exits the sub. If it does not, it adds it and runs a macro.

The code:
Code:
Sub e8a2ad2182454361bba1f17debd02f3b_Click()
Dim Reference As Object
          With ThisWorkbook.VBProject
           For Each Reference In .References
                 If Reference.Description Like "Reporter Bridge" Then Exit Sub
           Next
           .References.AddFromGuid "{F8E5D4DE-ED63-43DE-A82F-68DC97B68A5E}", 2, 4
     End With
     
Call Bubba
End Sub

Thanks!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try this adapted code (I assumed the macro you want to run is called Bubba? Funny naming conventions you use :biggrin:)

Code:
Sub e8a2ad2182454361bba1f17debd02f3b_Click()
   Dim Reference As Object
   Dim ReferenceIsMissing as Boolean
   ReferenceIsMissing = True
   With ThisWorkbook.VBProject
      For Each Reference In .References
         If Reference.Description Like "Reporter Bridge" Then 
            ReferenceIsMissing = False
         End If
      Next
      If ReferenceIsMissing Then
         .References.AddFromGuid "{F8E5D4DE-ED63-43DE-A82F-68DC97B68A5E}", 2, 4
      End If
   End With
   Call Bubba
End Sub
 
Upvote 0
Code:
Sub e8a2ad2182454361bba1f17debd02f3b_Click()

Dim Reference As Object

  With ThisWorkbook.VBProject
     For Each Reference In .References
         If Reference.Description Like "Reporter Bridge" Then 
[COLOR=green]             ' found - call Bubba and exit[/COLOR]
             Call Bubba
             Exit Sub
         End If
     Next
[COLOR=green]     ' not found - add reference, then call Bubba and exit
[/COLOR]     .References.AddFromGuid "{F8E5D4DE-ED63-43DE-A82F-68DC97B68A5E}", 2, 4
 End With
     
Call Bubba

End Sub
 
Upvote 0
Thanks both of you for taking time to try to help.

Ruddles, yours was one of the ways I was trying myself, and it keeps getting an error at ".References.AddFromGuid "{F8E5D4DE-ED63-43DE-A82F-68DC97B68A5E}", 2, 4"

Hermanito, yours works beautifully! I wouldn't have thought to do it quite like that, which is why I came here for help:). As far as the name "Bubba", I just threw that into the post, rather than the actual random letters and numbers that are actually assigned to the action that is being performed.

Thanks!
Simico
 
Upvote 0
I find it strange that Ruddles' solution doesn't work for you...

Apart from the fact that my code is elegant and robust, while his is more spaghetti-like :biggrin::biggrin:, they are actually doing the same thing.

Glad you got it working for you...
 
Upvote 0
Yes, in my defence I thought changing:-
Code:
If Reference.Description Like "Reporter Bridge" Then Exit Sub
to:-
Code:
If Reference.Description Like "Reporter Bridge" Then 
   Call Bubba
   Exit Sub
End If
was the smallest possible modification to the existing code and that the OP would be able to follow it more clearly that way.

As for 'spaghetti code':-
  • Italians like spaghetti!
  • I always reckon Exit Subs are allowed. GoTos are not!
:)
 
Upvote 0
I'll give you that :biggrin:

You chose indeed the smallest change, and much of the spaghetti-ism was inherited from simico :rofl:

I defend my solution because:
- it is more self-explanatory about what is happening inside the sub
- it works :stickouttounge:

still wondering though why yours doesn't work for simico, I can't see the reason (which is another reason my code is better, yours is too difficult to debug :laugh:)
 
Upvote 0

Forum statistics

Threads
1,224,537
Messages
6,179,405
Members
452,911
Latest member
a_barila

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