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
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”.
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.
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.
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:
- Reasons And Solutions for Excel Object Required Error in VBA
- [Fixed!] Excel Application-Defined or Object-Defined Error in VBA
- How to Fix Compile Error in Hidden Module in Excel
- [Fixed!] Invalid Forward Reference in VBA
- [Fixed!] Unable to Set the Visible Property of the Worksheet Class
- Because of Your Security Settings Macros Have Been Disabled
- Sub or Function Not Defined in Excel VBA
- [Solved!] Excel VBA “Argument Not Optional” Error
- [Fixed!] Method Open of Object Workbooks Failed