用户定义的类型未定义

2023/04/07

您可在 Visual Basic 中创建自己的 数据类型 . ,但它们必须先在 Type…End Type 语句或之前注册的 对象库 . 或 类型库 . 中定义。 此错误的原因及解决方案如下:

  • 您已尝试声明未定义数据类型的 变量 . 或 参数 . 或您指定了未知 类 . 或对象。

    在 模块 . 中使用 Type 语句定义新的数据类型。 如果您尝试创建对类的引用,则类必须对 项目 . 可见。 如果您要在程序中引用类,则项目中必须具有指定名称的 类模块 . 。 检查类型名称或对象名称的拼写。

  • 您要声明的类型位于其他模块中,但已声明为 Private 。 将此类型的定义移到 标准模块 . ,此类型在其中可声明为 Public

  • The type is a valid type, but the object library or type library in which it is defined isn’t registered in Visual Basic. Display the References dialog box, and then select the appropriate object library or type library. For example, if you don’t check the Data Access Object in the References dialog box, types like Database, Recordset, and TableDef aren’t recognized and references to them in code cause this error.

    该类型是一个有效的类型,但是定义该类型的对象库或类型库未在 Visual Basic 中注册。请显示 “引用” 对话框,然后选择适当的对象库或类型库。例如,如果您在 “引用” 对话框中未选中 “数据访问对象”,则像 Database、Recordset 和 TableDef 这样的类型将不被识别,代码中对它们的引用将导致此错误。

有关其他信息,选择有问题的项并按 F1(在 Windows 中)或 HELP(在 Macintosh 上)。

【VBA报错】编译错误:用户类型未定义

Shipley Leo于 2024-05-18 23:06:15 发布

一、报错问题

直接使用Dictionary对象,会报错“编译错误:用户类型未定义”,如下:

Sub testDict()
    Dim Dict As Dictionary '声明字典
    Set Dict = New Dictionary   '创建字典对象
    '场景一:给字典赋值
    Dict(0) = "姓名"                    '设置字典key为数字0,value为"姓名"
    Dict(Dict(0)) = "张三"              '设置字典key为Dict(0),value为"张三"
    Debug.Print Dict(0), Dict(Dict(0))  '姓名          张三
    '场景二:RemoveAll
    Dict.RemoveAll                      '清空字典
    Dict(0) = "爱好"
    Dict(Dict(0)) = "跑步"
    Debug.Print Dict(0), Dict(Dict(0))  '爱好          跑步
    '场景三:使用Set设置变量 Dict("childDict") 为字典对象
    Set Dict("childDict") = Dict        '设置字典key为"childDict",value为字典的引用
    Debug.Print Dict("childDict")(0), Dict(Dict("childDict")(0))    '爱好          跑步
End Sub

三、原因分析

vba 中没有添加引用动态链接库 scrrun.dll (对应Microsoft Scripting Runtime)。

四、解决方案

需要手动添加对应的动态链接库 scrrun.dll (对应Microsoft Scripting Runtime)。具体操作如下:

在菜单栏找到 “工具 > 引用”,点击进去后,找到 Microsoft Scripting Runtime 并勾选,接着点击【确定】按钮就可以正常使用。如下图所示:

1)工程——引用——选中"Microsoft ActiveX Data Objects 2.6 Library"——点击“确定” 拓展:ActiveX DataObjects,简称ADO,访问数据库的一个接口,就是用来跟数据库交互的。 2)检查定义的代码是否有问题,要保证上下文的定义代码一致 3)"Ct...
执行报错,报--------------------------- Microsoft Visual Basic for Applications --------------------------- 编译错误: 子过程或函数未定义 --------------------------- 确定 帮助 ---------------------------
This error may apear if Visual Basic for Applications (VBA) was not installed, or was set to Install on first use, (advertised, or set to install on demand). Depending on the installation mode used and the user rights to change current settings, it is possible to install VBA through Control Panel, Add or Remove Programs, Microsoft Office, Change, Add or Remove Features. If Visual Basic for Applications is not listed as an installable feature of Microsft Office, your system administrator may have disabled it so that you cannot install it. If VBA is not installed, you cannot run VBA-based programs or projects on this computer.
Symptom: When compiling a Visual Basic 6 application the following error is generated when VB encounters the type "IUnknown": Compiler error: User-defined type not defined Cause: This err
Microsoft Visual Basic for Applications 编译错误:更新 Declare 语句 Microsoft Visual Basic for Applications 编译错误: 编译错误: 若要在 64 位系统上使用,则必须更新此项目中的代码。请检查并更新 Declare 语句,然后用 PtrSafe 属性标记它们 在Declare 后面加PtrSafe 进行标记,如图所示 Windows 安全中心:页面不可用 你的 IT 管理员已限制对此应用的某些区域的访问,并且你尝试访问的项目不可用。有关详细信息,请与 IT 支持人员联系。/ UUP dump 查询错误 78915 VMware Workstation 此平台不支持虚拟化的 Intel VT-x/EPT …不支持嵌套虚拟化。模块“HV”启动失败…不支持使用虚拟化性能计数器。模块“VPMC“启动失败 35005