VBA枚举有时会给出“需要恒定表达".错误 [英] VBA enums give occasional "Constant Expression Required" errors

查看:90
本文介绍了VBA枚举有时会给出“需要恒定表达".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Excel VBA的类模块中定义的枚举.一切正常,但是每次对枚举变量进行比较时,我都开始遇到编译错误:

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:

在C类示例中:

Enum MyEnum
    Foo
    Bar
End Enum

其他地方:

If someValue = myEnum.Foo Then

文本.Foo将突出显示,并弹出编译错误:需要常量表达式"消息.

The text .Foo will be highlighted, and a "Compile error: Constant expression required" message pops up.

在Google上进行的搜索表明,这种情况可能会随机发生,并进行了一些修复,例如重新启动IDE或在枚举声明后添加空格,可以使其重新开始工作.

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.

  • http://www.tek-tips.com/viewthread.cfm?qid=1355882
  • http://www.vbforums.com/showthread.php?405564-RESOLVED-Constant-Expression-Required-Error-when-checking-Enum

这真的是VBA中的已知错误吗?我有什么办法可以避免这种情况的发生,或者如果VBA确实出现了,可以可靠地使VBA重新工作吗?

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?

就我而言,关闭并重新打开Excel并没有帮助.对不起,当我重启PC时.

In my case, closing and reopening Excel hasn't helped. Excuse me while I reboot my PC.

重启后更新:

重启机器后问题仍然存在,这令人惊讶.我尝试在枚举定义前添加Public(它们应为

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 default but 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.

看起来确实是VBA中的一个随机错误.我想知道经验丰富的开发人员是否经常发现这种情况-您建议不要使用枚举吗?还是会在蓝色的月亮中突然弹出一次,而我却很不幸?

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?

经过6周的开发后更新:

在我剩余的时间里,这个问题没有再出现,所以看来这是一个罕见的问题.

The problem didn't recur during the rest of my time developing this project, so it looks like it is a rare problem.

推荐答案

如问题中所述,我通过编辑并保存枚举定义,然后撤消编辑并保存来摆脱了错误.最近在该项目上做了更多工作之后,我发现了一个不同但又相似的问题-一行代码会出现类型不匹配"错误,其中没有类型不匹配,并且相同的功能未发生变化,可以正常使用相同的输入.

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.

我看到的一些间歇性错误可能是由于Excel文件中的代码伪像的堆积-经过阅读后,我发现VBA代码已编译并保存到文件中.没有清除"或全部重建"选项-VBA会尝试自行解决需要进行哪些增量更改.在您进行了很多代码更改的项目中,这可能导致各种奇怪的运行时行为.这可能是我在本工作簿的最初开发过程中发现的枚举错误的原因. "这意味着什么在VBA中进行反编译和压缩".本文对此进行了很好的概述.

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.

对此问题的大多数提及建议使用VBA CodeCleaner: http://www.appspro.com/实用工具/CodeCleaner.htm .著名的VBA专家Chip Pearson说:我非常强烈地推荐这个插件."我很惊讶我之前从未遇到过!

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 strongly recommend this add-in". I'm surprised I haven't come across this before!

这篇关于VBA枚举有时会给出“需要恒定表达".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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