VBA:新收藏集->模块不是有效的类型 [英] VBA: New collection -> A module is not a valid type

查看:258
本文介绍了VBA:新收藏集->模块不是有效的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用集合作为函数的一部分,但是我不断收到错误消息:编译时模块不是有效类型".

I'm trying to use a collection as part of a function, however I keep getting the error: "A module is not a valid type" on compile.

即使该函数只是简单地定义了一个集合,我也会得到相同的结果:

Even if the function simply defines a collection, I get the same:

Function CountUniqueTags()
    Dim table As Collection
    Set table = New Collection
End Function

此代码在标准模块中,但是错误表明我应该在类模块中编写此代码,但是Collection是一个内置类,因此我看不到这个问题?

This code is in a standard module, but the error implies I should be writing this in a class module, but Collection is a built-in class so I don't see the issue?

推荐答案

这也让我发疯了一段时间.我在一个文件中有很好的代码.复制代码,转到另一个文件,添加并重命名模块,粘贴到代码中,突然代码将无法编译!在看到一个可以给我所需线索的答案之前,我读了几个无用的答案.

This was making me go crazy for a while, too. I had code in one file that worked fine. Copied the code, went to a different file, added and renamed the module, pasted in the code, and suddenly the code will not compile! I read several unhelpful answers before I saw one that gave me the clue I needed.

错误消息指出"模块不是有效的类型"(请注意单词模块").这意味着您指定的类型(在您的情况下为集合")也是模块的名称.

The error message states "A module is not a valid type" (note the emphasis on the word "module"). That means the Type you are specifying (in your case, "Collection") is also the name of a Module.

如果将模块集合"重命名为其他名称,该错误将消失.

If you rename the Module "Collection" to some other name, the error will go away.

这篇关于VBA:新收藏集->模块不是有效的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆