[Solved]: User Defined Type Not Defined in Excel VBA: 2 Quick Methods

Method 1 – Spelling Error While Declaring Variables

Look at the following lines of codes.

Sub User_Defined_Type_not_Defined()
Dim Name As Strng
Name = "ExcelDemy"
MsgBox Name
End Sub

VBA Code with the Error User Defined Type Not Defined in Excel VBA

Run this code, you’ll get an error box notifying you of the presence of the error “User-Defined Type Not Defined”. You’ll find the second line of the code highlighted in blue, where we wrote “strng” in place of “string”.

User Defined Type Not Defined Error in Excel VBA

I hope you get the point. VBA could not recognize the variable type “strng”, that’s why it considered it as a new variable type and thought you have defined it somewhere. But when it found you have not declared it anywhere, it raised an error.

While running simple VBA codes, if you encounter this error anyhow, first check all the spellings of your variable types.


Method 2 – Lack of Proper Referencing

Use some objects in our codes that need proper referencing from the Visual Basic Reference box. The VBA doesn’t recognize the object.

Look at the following VBA code.

Sub User_Defined_Type_not_Defined()
Dim MyDictionary As Dictionary
Set MyDictionary = CreateObject("Scripting.Dictionary")
End Sub

Run this code. Get the same old error “User-Defined Type not Defined”, highlighting the Dictionary object. This is because VBA couldn’t recognize the Dictionary object, and considered it a user-defined variable, which has not been defined mistakenly.

User Defined Type Not Defined Error in Excel VBA

How to solve this error? Easy. There are 2 ways to solve this error.

⧪ Solution 1: Checking the Prerequisite Reference from the Visual Box Reference Box

This is the smartest way to solve this problem. Go to Tools > References button in the Visual Basic ribbon. Click References.

Checking Reference to Solve User Defined Type Not Defined Error in Excel VBA

A dialogue box called References will open. Scroll and find Microsoft Scripting Runtime (This depends on the object that you are using. The Dictionary object needs Microsoft Scripting Runtime to be recognized. Using an object in a VBA code, you must know its prerequisite to be recognized by VBA.

Come back and run the code, because VBA can recognize the Dictionary object this time.

⧪ Solution 2: Declaring It as a Pure Object rather than a Specific Object

Declare the object as a pure object rather than a Dictionary object.

Notice the following lines of codes.

Sub User_Defined_Type_not_Defined()
Dim MyDictionary As Object
Set MyDictionary = CreateObject("Scripting.Dictionary")
End Sub

We declared the variable MyDictionary as a simple object, not a Dictionary. Run this code, you won’t get any error.


Download Practice Workbook

Download this practice workbook to exercise while you are reading this article.


Related Articles:

Get FREE Advanced Excel Exercises with Solutions!

Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo