Libraries add pre-defined code structures (such as functions, subroutines among others) in a simple and easy way.
In the VBA context, a library usually allows access to new objects and functions.
Libraries that are enabled by default when opening Excel:
Class: is what defines the structure of a given object (E.g. for class Range we have objects like Range("A1"), Range("C2:B5"), and so on).
To access, enable and disable libraries in VBA:
To add a library that is not in the list, use the button and select the file of the new library.
Enabling additional libraries allows you to develop codes that communicate with other applications and processes. E.g. Word, Outlook, DAO (Data Access Objects), ADOdb (Active Data Objects database), etc...
When we refer to a library in the code but forget to enable it, the following error message is displayed:
The Object Browser allows you to view the list of all the different objects, methods, and properties of the enabled libraries.
To search the libraries:
When in doubt, the Microsoft Help button () can elucidate the utility of the selected element.
You can use structures from a library without enabling it. This procedure is called Late Binding.
Dim OutApp As Object Set OutApp = CreateObject("Outlook.Application")
Usually for the Late Binding a variable is declared as an object and later associated with CreateObject.
When we enable a library to access its structures we are performing what is called Early Binding.
Dim OutApp As New Outlook.Application
In the example case the Outlook library was enabled ().
Early: From the point of view of code development and performance time, Early Binding is more suitable because it allows you to use Excel intellisense ( + ) as well as the library object browser.
Late: Regarding compatibility errors, mainly due to the use of different versions of Excel (which changes the version of the libraries), Late Binding is more indicated.
Intellisense: is an aid, mainly to autocomplete the code, with useful information ( + ).
So, to gain practice with the libraries, try to write the code with Early Binding and later change it into Late Binding.
SuperExcelVBA.com is learning website. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. All Rights Reserved.
Excel ® is a registered trademark of the Microsoft Corporation.
© 2023 SuperExcelVBA | ABOUT