VBA 枚举偶尔会出现“需要常量表达式”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13196909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA enums give occasional "Constant Expression Required" errors
提问by Neil Vass
I'm using an enum defined in a class module in Excel VBA. This has been working fine, but I've started getting a compile error on every time I do a comparison on enum variables:
我正在使用在 Excel VBA 的类模块中定义的枚举。这一直工作正常,但每次我对枚举变量进行比较时,我都开始收到编译错误:
In class CExample:
在类 CExample 中:
Enum MyEnum
Foo
Bar
End Enum
Elsewhere:
别处:
If someValue = myEnum.Foo Then
The text .Foo
will be highlighted, and a "Compile error: Constant expression required" message pops up.
文本.Foo
将突出显示,并弹出“编译错误:需要常量表达式”消息。
A search on Google suggests that this can randomly happen, and fixes such as restarting the IDE or adding a space after the enum declaration can make it start working again.
在 Google 上的搜索表明这可能会随机发生,并且诸如重新启动 IDE 或在枚举声明后添加空格之类的修复程序可以使其重新开始工作。
- http://www.tek-tips.com/viewthread.cfm?qid=1355882
- http://www.vbforums.com/showthread.php?405564-RESOLVED-Constant-Expression-Required-Error-when-checking-Enum
- http://www.tek-tips.com/viewthread.cfm?qid=1355882
- http://www.vbforums.com/showthread.php?405564-RESOLVED-Constant-Expression-Required-Error-when-checking-Enum
Is this really a known bug in VBA? Is there anything I can do to avoid it happening, or reliably get VBA working again if it does crop up?
这真的是 VBA 中的已知错误吗?我能做些什么来避免它发生,或者如果它突然出现,可以可靠地让 VBA 再次工作?
In my case, closing and reopening Excel hasn't helped. Excuse me while I reboot my PC.
就我而言,关闭并重新打开 Excel 没有帮助。对不起,我重新启动了我的电脑。
Update after reboot:
重启后更新:
The problem persisted after rebooting my machine, which is surprising. I tried adding Public
in front of the enum definition (they're meant to be public by defaultbut I thought I'd give it a try), and the error disappeared. I've removed the Public
keyword (so we're back to my original code) and it still compiles and runs fine.
重新启动机器后问题仍然存在,这令人惊讶。我尝试Public
在枚举定义之前添加(默认情况下它们应该是公开的,但我想我会尝试一下),并且错误消失了。我已经删除了Public
关键字(所以我们回到了我的原始代码),它仍然可以编译并运行良好。
It does look like this is a random bug in VBA. I'd be interested to know if experienced developers have found this comes up often - would you advise not using enums? Or does it pop up once in a blue moon and I was just unlucky?
看起来这确实是 VBA 中的一个随机错误。我很想知道有经验的开发人员是否经常发现这种情况 - 您是否建议不要使用枚举?或者它会在蓝色的月亮中突然出现一次而我只是不走运?
Update after 6 weeks of further development:
进一步开发 6 周后更新:
The problem didn't recur during the rest of my time developing this project, so it looks like it is a rare problem.
在我开发这个项目的剩余时间里,这个问题没有再次出现,所以看起来这是一个罕见的问题。
采纳答案by Neil Vass
As noted in the question, I got rid of the error by editing and saving the enum definition, then undoing the edit and saving again. Having recently done some more work on the project, I found a different but similar issue - one line of code would give a "Type mismatch" error, where there was no type mismatch and where the same function, unchanged, had been working fine with the same inputs.
如问题中所述,我通过编辑和保存枚举定义,然后撤消编辑并再次保存来消除错误。最近在该项目上做了一些更多的工作,我发现了一个不同但相似的问题 - 一行代码会给出“类型不匹配”错误,其中没有类型不匹配,并且相同的功能,未更改,一直可以正常工作相同的输入。
Some of the intermittent errors I'm seeing might be due to a buildup of code artefacts in the Excel file - having done some reading, I've found that VBA code gets compiled and saved into the file. There's no "clean" or "rebuild all" option - VBA tries to work out for itself what incremental changes are needed. This can lead to all kinds of odd runtime behaviour in projects where you've made lots of code changes. This is likely the cause of the enum errors I was finding during initial development of this workbook. The section "What It Means to Decompile and Compact in VBA" in this article gives a good overview.
我看到的一些间歇性错误可能是由于 Excel 文件中的代码人工制品的积累 - 阅读了一些资料后,我发现 VBA 代码被编译并保存到文件中。没有“清理”或“全部重建”选项——VBA 试图自己计算出需要哪些增量更改。这可能会在您进行大量代码更改的项目中导致各种奇怪的运行时行为。这可能是我在本工作簿的初始开发过程中发现的枚举错误的原因。本文中的“在 VBA 中反编译和压缩意味着什么”部分给出了很好的概述。
Most mentions of this problem recommend using VBA CodeCleaner: http://www.appspro.com/Utilities/CodeCleaner.htm. Chip Pearson, a well-known and respected VBA expert, says " I very stronglyrecommend this add-in". I'm surprised I haven't come across this before!
大多数提及此问题的建议使用 VBA CodeCleaner:http: //www.appspro.com/Utilities/CodeCleaner.htm。Chip Pearson 是著名且受人尊敬的 VBA 专家,他说“我非常强烈推荐这个加载项”。我很惊讶我以前没有遇到过这个!
回答by JustinJDavies
Seems to be a bug.
似乎是一个错误。
Copy the same module's code to a new one, and recompile. That seems to solve it for some.
将相同模块的代码复制到新的,然后重新编译。这似乎可以解决一些问题。
A similar fix exists, which involves editing and undoing on the enum definition's line.
存在类似的修复,它涉及在枚举定义的行上进行编辑和撤消。
Consider switching to numeric constants if this is a frequent problem.
如果这是一个常见问题,请考虑切换到数字常量。
回答by Thor
An old question but just experienced this. Removed Public definer on the Enum and it compiled just fine. Didn't restart IDE. Surprising this is still here.
一个老问题,但刚刚经历了这个。删除了枚举上的公共定义器,它编译得很好。没有重新启动IDE。令人惊讶的是,这仍然存在。
回答by PaulK
This error does now and then, when no changes were made to the enum or its use or any code related. What worked for me is to make the move the enum from the Class to a Module I have called 'Common' and make the enum Public instead of Private.
当没有对枚举或其使用或任何相关代码进行任何更改时,此错误会不时发生。对我有用的是将枚举从类移动到我称为“通用”的模块,并使枚举变为公共而不是私有。
回答by Dustin Snyder
I had the same issue with my Enum, I added Public Enum to the declaration and the problem stopped. No need to reboot.
我的 Enum 也有同样的问题,我在声明中添加了 Public Enum 并且问题停止了。无需重新启动。