Data Type - Object
This data type contains a reference (or an address) to an actual object.
This can be used to contain any type of object (or Reference Type).
This data type uses 4 bytes.
The default value is Nothing.
Dim myObject As Object
Avoid This Data Type
Using the generic Object data type is much less efficient than using the specific object declaration.
Whenever you use this data type it will always use late binding.
You can find out if an Object variable has been set by using the "Is Nothing" keywords.
It makes your code less readable.
The reasons for this is because Excel cannot tell what type of object the variable refers to until the program is running, which obviously consumes some execution time.
This is referred to as late binding and can make programs run significantly more slowly.
Late Binding
Using the Object data type will declare the variable using late binding.
Dim oWorkbook As Object
Early Binding
Dim oWorkbook As Workbook
Set Keyword
You cannot assign an object to a variable using an equals sign.
Instead you must always use the "Set" statement.
GetObject Function
GetObject - Returns the reference to an object provided by an ActiveX component.
This can be used when you want to access a current instance of an object.
This code adds a new workbook to the current Excel instance.
Dim oXLApp As Excel.Application
Dim oWbk As Excel.Workbook
Set oXLApp = GetObject(ThisWorkbook.Name).Application
Set oWbk = oXLApp.Workbooks.Add
CreateObject Function
CreateObject - Returns the reference after creating a new ActiveX or OLE Object.
This code can be usedfrom Word to create an instance of Excel using early binding.
Dim oXLApp As Excel.Application
Dim oWbk As Excel.Workbook
Set oXLApp = CreateObject("Excel.Application")
Set oWbk = oXLApp.Workbooks.Add
Passing Objects In
When you declare a variable with an Object data type it can assigned a value using the Set keyword.
Dim myObject As Object
Set myObject = ThisWorkbook
It is possible to pass an object into a subroutine or function
Call PassingIn(myObject)
Public Sub PassingIn(ByRef myObject As Object)
Debug.Print TypeName(myObject)
'Workbook
End Sub
Always By Reference
It doesn't matter if you pass in this variable using ByVal or ByRef, the object is always passed by reference.
This means that a reference pointer is passed in and not a copy of the actual object.
ByRef vs ByVal
There is a difference though between the following 2 lines
Public Sub PassingIn(ByRef myObject As Object)
Public Sub PassingIn(ByVal myObject As Object)
ByRef - Passes in the original reference pointer
ByVal - Passes in a copy of the reference pointer
The object will be modified in both cases
ByRef - Caller variable can be reassigned
ByVal - Caller variable cannot be reassigned
Example
Public Sub Testing()
Dim myObject As Object
Set myObject = Range("A1")
Call PassedByRef(myObject)
Debug.Print myObject.Address '$B$1 - changed
myObject.Value = 20
Set myObject = Range("A1")
Call PassedByRef(myObject)
Debug.Print myObject.Address '$A$1 - unchanged
myObject.Value = 20
End Sub
Public Sub PassedByRef(ByRef r As Range)
Set r = Range("B1")
End Sub
Public Sub PassedByVal(ByVal r As Range)
Set r = Range("C1")
End Sub
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext