VBA references - list, add & remove
In this module - how to programmatically:
- List available VBA references
- Add VBA references to the project
- Delete VBA references from the project
0. VBA References
The VBA reference list (figure 1) can be accessed by the VBE menu
sequence. The Available References list displays each reference item by its description property, in priority order.References are also part of the libraries drop-down in the Object Browser (figure 2). Each selected reference list item is shown by its name property, in alphabetical order, in the Object Browser.
1. VBA References - List
Code 1 uses the Reference object and its associated References collection, to print a list of available references. This code requires the Microsoft Visual Basic for Applications Extensibility 5.3 reference.
Code 1: macro
xlfVBEListReferences
print reference list to immediate window
Sub xlfVBEListReferences() ' Requires References :: Microsoft Visual Basic for Applications Extensibility 5.3 ' C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB Dim oRef As VBIDE.Reference ' Item Dim oRefs As VBIDE.References ' Collection Dim i As Integer Set oRefs = Application.VBE.ActiveVBProject.References Debug.Print "Print Time: " & Time & " :: Item - Name and Description" For Each oRef In oRefs i = i + 1 Debug.Print "Item " & i, oRef.Name, oRef.Description Next oRef Debug.Print vbNewLine i = 0 Debug.Print "Print Time: " & Time & " :: Item - Full Path" For Each oRef In oRefs i = i + 1 Debug.Print "Item " & i, oRef.FullPath Next oRef Debug.Print vbNewLine i = 0 ' List the Globally Unique Identifier (GUID) for each library referenced in the current project Debug.Print "Print Time: " & Time & " :: Item - GUID" For Each oRef In oRefs i = i + 1 Debug.Print "Item " & i, oRef.GUID Next oRef Debug.Print vbNewLine End Sub
About Code 1
- Line 14: Debug.Print - for each reference, print the Item number, Name and Description properties to the Immediate Window
- Line 22: Debug.Print - for each reference, print the Item number, and FullPath property to the Immediate Window
- Line 31: Debug.Print - for each reference, print the Item number, GUID (Globally Unique Identifier) property to the Immediate Window
- See figure 3 for the Immediate Window details
Reference objects, Items 1 and 9, are expanded in the Locals Window view of Figure 4 as the oRefs object from code 1.
2. VBA References - Add
The Reference items can be created in the References Collection with the AddFromFile (code 2a) or AddFromGuid (code 2b) methods.
Code 2a: macro
xlfVBEAddReferences
add a Reference object to the References collection
Sub xlfVBEAddReferences() Dim oRefs As References Set oRefs = Application.VBE.ActiveVBProject.References On Error GoTo OnError oRefs.AddFromFile "C:\Windows\System32\msxml6.dll" OnError: End Sub
Code 2b: macro
xlfVBEAddReferencesGUID
add a Reference object to the References collection
Sub xlfVBEAddReferencesGUID() Dim oRefs As References Set oRefs = Application.VBE.ActiveVBProject.References On Error GoTo OnError ' Syntax: AddFromGuid(Guid, Major, Minor) ' The Major version number of the reference. ' The Minor version number of the reference. ' Microsoft XML, v6.0 :: Major - 6, Minor - 0 oRefs.AddFromGuid "{F5078F18-C551-11D3-89B9-0000F81FE221}", 6, 0 OnError: End Sub
3. VBA References - Remove
To remove an item from the collection, loop through with For Each ... Next, and identify specific objects by Name (code 3a) or Description (code 3b) then Remove.
Code 3a: macro
xlfVBERemoveReference1
- reference name version
Sub xlfVBERemoveReference1() Dim oRef As Reference Dim oRefs As References Set oRefs = Application.VBE.ActiveVBProject.References For Each oRef In oRefs If oRef.Name = "MSXML2" Then oRefs.Remove oRef Exit For End If Next oRef End Sub
Code 3b: macro
xlfVBERemoveReference2
- reference description version
Sub xlfVBERemoveReference2() Dim oRef As Reference Dim oRefs As References Set oRefs = Application.VBE.ActiveVBProject.References For Each oRef In oRefs If oRef.Description = "Microsoft XML, v6.0" Then oRefs.Remove oRef Exit For End If Next oRef End Sub
- This example was developed in Excel 365 :: VBA 7.1
- Published: 16 July 2020
- Revised: Saturday 25th of February 2023 - 10:13 AM, [Australian Eastern Standard Time (EST)]