How to Fix Compile Error: User-defined Type Not Defined (VIDEO)
Are you sitting there staring at this error on your VBA screen and getting frustrated? No worries, we shall fix it.
But before deep diving into the root cause and solution to fix this error, let’s understand the correct procedure for using an object in our code. It will ease the debugging process.
For this example, let’s look at a Dictionary object.
DICTIONARY in VBA:
A DICTIONARY is an object similar to the VBA COLLECTION object with the following differences:
- The values of the keys can be updated or changed later and
- The key / item value can easily be checked for existence without completely iterating through all the items. This also helps to retrieve values easily.
If you’re a beginner, just imagine that this object is a real time dictionary where the keys are the words and items are the respective definitions. As in a dictionary, in the VBA object we do not need to iterate through all the keys to find the value of one specific key.
And just like any other object in VBA, we can use a dictionary object by adding the corresponding reference through Tools menu. Declaration and definition of objects can be done through early or late binding methods per the developer’s convenience.
Resolving the Error
The error in the title is a compile time error that is encountered when you compile the code.
Analyze the meaning and “ROOT CAUSE” of the error:
Let us split and read the error to understand it better.
User-defined type | not defined
First, let’s try to understand we have encountered the error because something is
A possible reason for the error to occur is that you are utilizing the early binding method to declare and define the object, but the required reference has not been added.
Refer to the sample code below to understand the difference between early and late binding.
' Create a dictionary object using the late binding method. Dim obdict As Object Set obdict = CreateObject("Scripting.Dictionary")
' Create a dictionary object using the early binding method. Dim obdict As New Scripting.Dictionary
Try one of the following steps to resolve the error:
Maybe VBA doesn’t understand that you have defined the object. In VBA, you need to add the respective reference for the object to let the language know that you have properly defined it.
- Goto the menu Tools-> References
- Select the library “Microsoft Scripting Runtime.” (This varies depending on the object used. Here the same dictionary object is considered for explanation purposes
- Click on the “OK” button and close the dialog
- Now you can compile the code and see that the error doesn’t appear anymore
Note: All this is NOT mandatory if you are following “late binding” method.
Use the late binding method where you declare a generic object first, then define its type. This does not require any reference.
Dim <variable> As Object
Set <variable> = CreateObject("Scripting.Dictionary")
Example for an Excel sheet object:
Dim ExcelSheet As Object
Set ExcelSheet = CreateObject("Excel.Sheet")
Example for a dictionary object:
'Example of creating a dictionary object
Dim odict As Object
Set odict = CreateObject("Scripting.Dictionary")
The video below shows how to resolve the error using each of the two methods above.