VBA – Early Late Binding
Written by
Reviewed by
In this Article
When an object is assigned to an object variable, VBA performs a process called binding. Objects can be early bound or late bound.
It’s easy to tell the difference: If you declare a variable as Object, you are late binding:
Dim xlApp As Object
If you explicitly declare the variable with a data type, you’re using early binding:
Dim xlApp As Excel.Application
Run-Time vs Compile-Time
To understand the difference between Early and Late binding, it’s important to first understand the difference between run-time and compile-time.
Compile-Time refers to the time before the code runs, VBA will “compile” the code, testing it for compile-time errors, allocating memory, etc. You can force VBA to compile your code at anytime from the Debug menu:
Run-Time refers to the time while the code runs.
Early Binding
Early binding links the object references during compile time. This has several advantages:
- Speed / Performance – Early binding generally offers better performance. Optimizations and memory allocations occur during compile time, reducing the runtime overhead.
- IntelliSense Support – When using Early Binding, IntelliSense is available. IntelliSense shows you the properties, objects, and methods available with the object, making writing code much easier.:
- Type Checking – Early Binding allows for compile-time checking, reducing the likelihood of run-time errors.
Early Binding does have at least one big disadvantage, which is discussed in the Late Binding section below.
Early Binding Example
Notice below that the variable xlApp is declared as an Excel.Application variable.
'Early Binding Sub earlybinding() 'Create variable to hold new Excel App Dim xlApp As Excel.Application 'Assign Excel App to variable Set xlApp = New Excel.Application 'Add Workbook to xlApp & Make xlApp Visible xlApp.Workbooks.Add xlApp.Visible = True End Sub
Late Binding
With late binding, variables are created as type Object at compile-time, resolving object references at run-time instead.
Advantages of Late Binding:
- No Need for Type Libraries – Late binding eliminates the need for referencing type libraries explicitly. This simplifies deployment and reduces the possbility of versioning issues.
- Flexibility – Late Binding offers more flexibility as object references are resolved at runtime.
- Dynamic Object Creation – Late Binding allows for dynamic creation of objects, allowing for scenarios where the type of object is determined at runtime. In other words, if you don’t know what type of object your code needs to handle (chart vs pivot table, etc.), use late binding.
Late Binding Example
Notice in this example, xlApp is declared as an Object. It’s type is then set later with the Set keyword.
'Late Binding
Sub latebinding()
'Create variable to hold new object
Dim xlApp As Object
'Assign Excel app to Object
Set xlApp = CreateObject("Excel.Application")
'Add Workbook to xlApp & Make xlApp Visible
xlApp.Workbooks.Add
xlApp.Visible = True
End Sub